public class ImageUtil {
public static final String TAG = ImageUtil.class.getSimpleName();
/**
* 判断图片是否需要翻转(正方向的不用翻转)
*
* @param fileName
* @return
*/
public static boolean needRotate(Context context, String fileName) {
try {
ExifInterface exif = new ExifInterface(fileName);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
LogUtil.d(TAG, "orientation is " + orientation);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return true;
case ExifInterface.ORIENTATION_ROTATE_270:
return true;
case ExifInterface.ORIENTATION_ROTATE_180:
return true;
}
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 获取翻转到正方向的的图片
*
* @param fileName
* @param maxWidth
* @param maxHeight
* @return
*/
public static void rotatedBitmap(Context context, String fileName,
String dstFile) {
try {
ExifInterface exif = new ExifInterface(fileName);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
LogUtil.d(TAG, "orientation is " + orientation);
Matrix matrix = null;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix = new Matrix();
matrix.postRotate(90);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix = new Matrix();
matrix.postRotate(270);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix = new Matrix();
matrix.postRotate(180);
break;
}
Bitmap bmp = getSmallBitmap(context,
Uri.fromFile(new File(fileName)));
if (matrix != null) {
Bitmap bmp2 = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), matrix, true);
bmp.recycle();
FileOutputStream fos = new FileOutputStream(dstFile);
bmp2.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
bmp2.recycle();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean compress(Context context, Uri src, File dst, int rate) {
try {
Bitmap bmp = getSmallBitmap(context, src);
FileOutputStream fos = new FileOutputStream(dst);
bmp.compress(Bitmap.CompressFormat.JPEG, rate, fos);
bmp.recycle();
bmp = null;
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
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 Bitmap getSmallBitmap(Context context, Uri uri) {
try {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream is = context.getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(is, null, options);
is.close();
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 720, 1280);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
is = context.getContentResolver().openInputStream(uri);
Bitmap bmp = BitmapFactory.decodeStream(is, null, options);
is.close();
return bmp;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
分享到:
相关推荐
java处理图片ios拍照上传,图片被旋转。前提:在任何对图片处理之前调用此方法
android 解决部分手机拍照旋转问题
java 图片压缩 iphone拍照上传旋转问题处理,压缩工具类 首先导入jar 包,通过imgxz获取图片是否旋转属性,在调用旋转方法,旋转过来,然后在进行压缩
ios13.4及以上拍照上传图片旋转问题。
看到网上有很多解决关于拍照后图片旋转的问题,特别是三星的机型问题最大,但是都不能很好的解决问题,所以花时间研究了下,终于解决了拍照后图片旋转的问题,再此把代码贴出来跟大家一起研究下,有什么不对的地方还...
调用Android系统拍照功能后,三星手机拍摄后的照片被旋转了90度,横着拍给你变成竖的,竖的拍给你变成横的。其它品牌的手机都是正常的,就三星出现这个怪事。 在Android适配上,我原来一直以为国内的小米手机够奇葩...
android 图片 拍照 删除 添加 旋转 @Override ... // 向右旋转图片按钮 xuanzhuan = (Button) this.findViewById(R.id.xuanzhuan); xuanzhuan.setOnClickListener(new MyXuanZhuanClick(90)); }
早几天在开放中遇到三星照相旋转90度,上网去找资源都没有完整的资源。之后自己尝试多种方法实现了。希望能帮助到大家
android相机拍照,存储 调用相册, 图片旋转,裁剪
项目请参见:https://handsome-man.blog.csdn.net/article/details/116427984 图像平移是指将图像中所有的点都沿着水平或垂直方向移动一定的距离。 项目可直接运行~
一款自定义多选相册、单张图片编辑、旋转、裁剪功能
上传图片压缩并且转为base64并且上传阿里云(对ios相机拍照旋转90度处理)
利用exif.js解决IOS手机H5页面拍照上传照片旋转90度问题: upLoadPic($inputBorn, $inpOutBorn, 1); 其中$inputBorn为type="file"的input, $inpOutBorn为展示图片用的盒子,盒子要给宽高,默认的情况下,图片是以背景...
exif.js是一个JavaScript库,用于从图像文件读取EXIF元数据。解决ios拍照出现旋转的问题
今天开发一个拍照获取照片的功能的时候, 发现上传之后图片会自动旋转90.在测试中发现只要是图片大于2M, 系统就会自动翻转照片。下面小编通过本文给大家分享下解决办法
HTML5的file文件上传功能,在页面上配合canvas即可完成拍照上传图片的问题,但是会出现图片旋转的问题。
主要介绍了vue2实现移动端上传、预览、压缩图片解决拍照旋转问题,需要的朋友可以参考下
demo适合大多数项目使用,调用比较简单。包括 拍照 ,选择相册返回 图片旋转 切割 倒影 黑白 效果等功能。
部分手机拍照时,正面拍照照片会旋转 解决问题:使用exif.js来获取图像数据,然后进行处理 ** 1. 安装依赖 npm install exif-js --save 2. 获取图像数据 EXIF.getData(); 获取某个数据方向参数 EXIF.getTag(this, '...
Android调用系统相机拍照、实现压缩、图片旋转、保存以及6.0以上适配问题