100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Android照相机拍照Camera

Android照相机拍照Camera

时间:2019-07-28 06:49:21

相关推荐

Android照相机拍照Camera

Camera、1.权限设置2.intent、startActivityForResult启动照相3.onActivityResult 接收照相的返回值3.1、数据图片保dsdcard3.2、读取sdcard图片3.3、图片加载到imageView上4.点击图片;popupwindow或者新页面放大展示/*** 1.启动系统的相机拍照*/private void initPhoto() {// 隐式启动 系统相机Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, 1001);//注意重写:onActivityResult}@Override//2.protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == 1001) {if (null != data && data != null && data.getExtras() != null) {try {String path = saveImg(data);Bitmap bitins = getSdcardImg(path);ivLogo.setImageBitmap(bitins);} catch (Exception e) {e.printStackTrace();}}}super.onActivityResult(requestCode, resultCode, data);}/*** 3.1* @方法用于把图片存到本地* @param data 拍照intent返回到图片数据集* @return返回图片存储本地后的 、详细图片地址* @throws FileNotFoundException io操作的异常*/private String saveImg(Intent data) throws FileNotFoundException {Bitmap bit = (Bitmap) data.getExtras().get("data");// 得到照相拍照的图片String fileName = System.currentTimeMillis() + "";// 保存图片 路径 名称String path = Environment.getExternalStorageDirectory() + "/" + fileName + ".jpg";// 图片路径存入数据库,用于不同用户登录使用// 输出流 保存到本地sdcardFileOutputStream fos = new FileOutputStream(path);// 1.格式 2.质量 3. 输出流保存位置boolean b = press(CompressFormat.JPEG, 100, fos);if (b) {Toast.makeText(this, " img save successful", Toast.LENGTH_LONG).show();} else {Toast.makeText(this, " img save false ", Toast.LENGTH_LONG).show();}return path;}/*** 3.2* @param path 本地图片地址* @return 返回得到的 Bitmap 图片对象* @throws FileNotFoundException io读取异常*/private Bitmap getSdcardImg(String path) throws FileNotFoundException{FileInputStream fis = new FileInputStream(path);Bitmap bitins = BitmapFactory.decodeStream(fis);return bitins;}//给定图片宽高,返回压缩后的图片public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) width / w); float scaleHeight = ((float) height / h); matrix.postScale(scaleWidth, scaleHeight); // 不改变原来图像大小 Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); return newbmp; } //Activity Send Bitmap To FragmentFragmentManager fm = getFragmentManager();FragmentTransaction tran = fm.beginTransaction();ImageFragment imgFragment = new ImageFragment();Bundle b = new Bundle();b.putParcelable("bit", bitins);imgFragment.setArguments(b);tran.replace(R.id.rl_layout, imgFragment);mit();@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {Bitmap b = (Bitmap) getArguments().get("bit");ImageView iv = new ImageView(mContext);Log.e(TAG, "====onCreateView========");iv.setImageBitmap(b);iv.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));return iv;}照相机拍照需要的权限:<!--写sd卡权限--><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!--读sd卡的权限 --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><!--操作摄像头的权限 --><uses-permission android:name="android.permission.CAMERA" />

项目demo下载地址:

/detail/flyingsir_zw/9693403

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。