您的位置:首页 > 其它

解决三星手机拍照后,图片旋转。

2016-05-06 17:36 316 查看
最近项目中遇到用三星手机拍照,图片会自动旋转,应该是三星内部系统的功能,然后需要是不让他旋转,找到了方法。

原理就是,获取到图片,判断它的旋转角度,然后相应的旋转回来。

在拍照的返回结果中,获取到图片的路径。即activityforresult方法中。

path = filePath + fileName; //path 为拍照返回的路径

File file = new File(path);

int degree = readPictureDegree(file.getAbsolutePath());

Bitmap smallBitmap=FileUtils.compressSize(path, 800, 800);

smallBitmap = rotaingImageView(degree, smallBitmap);

saveAllPath = FileUtils.saveBitmap(smallBitmap, System.currentTimeMillis() +(Math.random() * 10000) + ".png");//质量压缩并存储得到path

ImageItem takePhoto = new ImageItem();

takePhoto.setBitmap(smallBitmap);

takePhoto.setImagePath(saveAllPath);

Bimp.tempSelectBitmap.add(takePhoto);

/**

* 尺寸压缩

* @param path 图片绝对路径

* @return degree旋转的角度

*/
public static Bitmap compressSize(final String path,final int w,final int h){

//Looper.prepare();

// TODO Auto-generated method stub

BitmapFactory.Options opts = new BitmapFactory.Options();

// 设置为ture只获取图片大小

opts.inJustDecodeBounds = true;//只读边,不读内容

opts.inPreferredConfig = Bitmap.Config.ARGB_8888;

// 返回为空

BitmapFactory.decodeFile(path, opts);

int width = opts.outWidth;

int height = opts.outHeight;

// 判断后缀名

String suffix = "";

CompressFormat format = null;

if (path.endsWith(".jpg")) {

suffix = ".jpg";

format = CompressFormat.JPEG;

} else if (path.endsWith(".jpeg")) {

suffix = ".jpeg";

format = CompressFormat.JPEG;

} else if (path.endsWith(".png")) {

suffix = ".png";

format = CompressFormat.PNG;

} else {

suffix = ".jpg";

format = CompressFormat.JPEG;

}

float scaleWidth = 0.f, scaleHeight = 0.f;

if (width > w || height > h) {//如果宽度大于 传入的宽度 或者 高度大于 传入的高度大于

// 缩放

scaleWidth = ((float) width) / w;

scaleHeight = ((float) height) / h;

}

opts.inJustDecodeBounds = false;

//缩放后的高度和宽度取最大值

float scale = Math.max(scaleWidth, scaleHeight);

opts.inSampleSize = (int) scale;//此处是最后的宽高值

Bitmap bMapRotate = BitmapFactory.decodeFile(path, opts);

if (bMapRotate!=null) {

return bMapRotate;

}

return null;

}

/**

* 读取图片属性:旋转的角度

* @param path 图片绝对路径

* @return degree旋转的角度

*/

public static int readPictureDegree(String path) {

int degree = 0;

try {

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 rotaingImageView(int angle , Bitmap bitmap) {

//旋转图片 动作

Matrix matrix = new Matrix();

;

matrix.postRotate(angle);

System.out.println("angle2=" + angle);

// 创建新的图片

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,

bitmap.getWidth(), bitmap.getHeight(), matrix, true);

return resizedBitmap;

}

/**

* 存储图片,返回路径

* @param bm

* @param picName

* @return

*/

public static String saveBitmap(Bitmap bm, String picName) {

try {

if (!isFileExist(picName)) {

File tempf = createSDDir("");

}

File f = new File(SDPATH, picName);

if (f.exists()) {

f.delete();

}

FileOutputStream out = new FileOutputStream(f);

bm.compress(Bitmap.CompressFormat.PNG, 100, out);

out.flush();

out.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return SDPATH + picName;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: