您的位置:首页 > 移动开发 > Android开发

Android 相机在Portrait模式下照相保存照片

2012-07-13 13:26 477 查看
在使用android的camera的时候会遇到两个问题,一个是camera在preview的时候orientation的问题,第二个就是在takePicture之后回遇到保存下来的图片旋转90度的问题

先解决第一个preview的orientation的问题,第一:在android2.2与以后的sdk版本中camera的orientation都是用的landscape,如果你的activity的screenOrientation设置成landscape的话,就不会有这个问题;第二:如果你的activity必须用portrait,那么可以在调用camera.open()方法之后调用下面这个方法来解决这个问题,

camera.setDisplayOrientation(90);


如果你用的sdk是2.2之前的话,可以这样解决:

Method rotateMethod = android.hardware.Camera.class.getMethod("setDisplayOrientation", int.class);
rotateMethod.invoke(camera, 90);


下面就是如何解决保存Portrait模式下图片旋转90度的问题

下面的程序在onPictureTaken方法里面执行

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 6;
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
options.inTempStorage = new byte[32 * 1024];
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bMap;
bMap = BitmapFactory.decodeByteArray(imgData[0], 0, imgData[0].length, options);
if(bMap.getHeight() < bMap.getWidth()){
orientation = 90;
} else {
orientation = 0;
}

Bitmap bMapRotate;
if (orientation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(),
bMap.getHeight(), matrix, true);
} else
bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(),
bMap.getHeight(), true);

FileOutputStream out;
try {
File imgFile = new File("/xxxx/xxx/snap.jpeg");
out = new FileOutputStream(imgFile);
bMapRotate.compress(Bitmap.CompressFormat.JPEG, 90, out);
if (bMapRotate != null) {
bMapRotate.recycle();
bMapRotate = null;
}
camera.startPreview();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐