Android 相机在Portrait模式下照相保存照片
在使用android的camera的时候会遇到两个问题,一个是camera在preview的时候orientation的问题,第二个就是在takePicture之后回遇到保存下来的图片旋转90度的问题
先解决第一个preview的orientation的问题,第一:在android2.2与以后的sdk版本中camera的orientation都是用的landscape,如果你的activity的screenOrientation设置成landscape的话,就不会有这个问题;第二:如果你的activity必须用portrait,那么可以在调用camera.open()方法之后调用下面这个方法来解决这个问题,
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(); }