手机拍照或者相册获取图片总结
来源:互联网 发布:朗文和牛津 知乎 编辑:程序博客网 时间:2024/05/01 15:23
注意:三星手机中会出现照片旋转,进而引起返回数据时出现的activity生命周期被重置的问题,在清单文件中添加一下代码就可以:android:configChanges="orientation|keyboardHidden|screenSize"/** * 从相册选择原生的照片(不裁切) */public static void selectPhotoFromGallery(BrandCarBaseActivity activity, int requestCode) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_PICK); //从所有图片中进行选择 intent.setType("image/*"); activity.startActivityForResult(intent, requestCode);}/** * 拍取照片不裁切 */public static void selectPhotoFromTake(BrandCarBaseActivity activity, String fiePath, int requestCode) { Intent intent = new Intent(); //设置Action为拍照 intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); //将拍取的照片保存到指定URI intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(fiePath))); activity.startActivityForResult(intent, requestCode);}/** * 采样率(等比)压缩 * 根据路径获得图片并压缩,返回bitmap用于显示 * * @param filePath 图片路径 * @param reqHeight 照片压缩后的height * @param reqWidth 照片压缩后的width * @return */public static Bitmap getCompressBitmap(String filePath, int reqHeight, int reqWidth) { final BitmapFactory.Options options = new BitmapFactory.Options(); if (filePath != null) { options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); options.inSampleSize = caculateInSampleSize(options, reqHeight, reqWidth); options.inPreferredConfig = Bitmap.Config.RGB_565; options.inJustDecodeBounds = false; return rotateBitmapByDegree(BitmapFactory.decodeFile(filePath, options), getBitmapDegree(filePath)); } else { return null; }}/** * 采样率(等比)压缩 * * @param uri 图片uri * @param context * @return */public static Bitmap getCompressBitmapFromUri(Context context, Uri uri, int reqHeight, int reqWidth) { return getCompressBitmap(getPathByUri(context, uri), reqHeight, reqWidth);}/** * 质量压缩并存储 * * @param bitmap * @param outFilePath 输出路径 * @param outSize 压缩后输出大小 kb */public static void compressAndSaveImage(final Bitmap bitmap, final String outFilePath, final int outSize) { if (bitmap == null){ return; } new Thread(new Runnable() { @Override public void run() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int options = 100; //质量压缩方法,把压缩后的数据存放到baos中 (100表示不压缩,0表示压缩到最小) bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos); // 循环判断如果压缩后图片是否大于100kb,大于继续压缩 while (baos.toByteArray().length / 1024 > outSize) { // 重置baos即让下一次的写入覆盖之前的内容 baos.reset(); options -= 10; if (options < 0) { options = 0; } bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos); if (options == 0) { break; } } try { FileOutputStream fos = new FileOutputStream(new File(outFilePath)); fos.write(baos.toByteArray()); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }).start();}/** * 质量压缩并存储 * * @param bitmap * @param outFilePath 输出路径 */public static void saveImage(final Bitmap bitmap, final String outFilePath) { if (bitmap == null){ return; } new Thread(new Runnable() { @Override public void run() { try { int options = 100; FileOutputStream fos = new FileOutputStream(new File(outFilePath)); bitmap.compress(Bitmap.CompressFormat.JPEG, options, fos); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }).start();}/** * 计算图片缩放值 * * @param options * @param reqWidth * @param reqHeight * @return */public static int caculateInSampleSize(BitmapFactory.Options options, int reqHeight, int reqWidth) { final int width = options.outWidth; final int height = options.outHeight; int inSampleSize = 1; if (width > reqWidth || height > reqHeight) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize;}public static String getPathByUri(Context context, Uri uri) { if ("content".equalsIgnoreCase(uri.getScheme())) { return getDataColumn(context, uri); } else { File file = new File(uri.getPath()); if (file.exists()) { return uri.getPath(); } } return null;}/** * uri路径查询字段 * * @param context * @param uri * @return */public static String getDataColumn(Context context, Uri uri) { Cursor cursor = null; final String column = "_data"; final String[] projection = {column}; try { cursor = context.getContentResolver().query(uri, projection, null, null, null); if (cursor != null && cursor.moveToFirst()) { final int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(index); } } finally { if (cursor != null) cursor.close(); } return null;}/** * 读取图片的旋转的角度 * * @param path 图片绝对路径 * @return 图片的旋转角度 */public static int getBitmapDegree(String path) { int degree = 0; try { // 从指定路径下读取图片,并获取其EXIF信息 ExifInterface exifInterface = new ExifInterface(path); // 获取图片的旋转信息 int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree;}public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) { Bitmap returnBm = null; try { // 将原始图片按照旋转矩阵进行旋转,并得到新的图片 if (bm != null) { // 根据旋转角度,生成旋转矩阵 Matrix matrix = new Matrix(); matrix.postRotate(degree); returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); } } catch (OutOfMemoryError e) { } if (returnBm == null) { returnBm = bm; } if (bm != returnBm) { bm.recycle(); } return returnBm;}
获得相册或者拍照后数据处理方法:
//REQ_WIDTH和REQ_HEIGHT 是期望得到照片的大小if (data.getData() == null) return;if (data.getData().getPath().startsWith("file")) { toastMsg(getResourceString(R.string.please_select_native_pic)); return;}Bitmap bm = getCompressBitmapFromUri(this, data.getData(), REQ_WIDTH, REQ_HEIGHT);
0 0
- 手机拍照或者相册获取图片总结
- Unity3D,获取相册图片,手机拍照
- 读取手机相册,从手机相册读取图片剪切,拍照获取图片,获取拍照图片进行剪切
- 手机拍照与相册获取
- 从相册\拍照获取图片
- 获取相册图片或拍照
- 苹果拍照或者获取相册簿UIImagePickerController
- Android开发,调用相册图片或者调用相机进行拍照获取图片
- 调用系统相机拍照获取图片或者从相册中调用本地图片显示问题
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- android 手机拍照。。相册 取图片
- 拍照,相册获取图片,高仿微信裁切图片
- Android实现类似QQ头像点击打开相册或者拍照,并获取裁剪后的图片
- android 调用系统相册选取照片或者打开相机拍照获取图片
- android 调用系统相册选取照片或者打开相机拍照获取图片
- Android总结之打开手机相册获取图片
- Android 拍照/相册 获取图片裁剪显示
- hdu4055(dp)
- (Unity 编辑器扩充(一))右键设置父节点
- robotframework自动化测试安装配置
- Android仿IOS的AssistiveTouch的控件EasyTouch实现
- react-native--手绘直线(手势)
- 手机拍照或者相册获取图片总结
- C#中的抽象类和接口
- 首篇博客小记
- Android开发-如何快速实现分享功能
- 20161208 JS计时器和CSS3旋转属性闹钟
- Python/Java中文乱码的问题
- 微信小程序学习笔记(5)--------框架之视图层
- CSS经典BUG
- Android stuido 中的keystore