图像从正面安卓摄像头上下颠倒保存

时间:2015-02-23 04:53:08

标签: android camera

我试图从我的前置摄像头捕捉图像并将其保存到我的画廊中,它可以在后向摄像头的两个方向上完美地工作PORTRAIT和LANDSCAPE 但是使用前置摄像头,它只适用于风景,

  

如果它是PORTRAIT,则图像上下旋转180度   我尝试了以下代码,因为它是最常见的解决方案,但没有任何作用

FileOutputStream fos;
                try {
                    fos = new FileOutputStream(fileName);
                    fos.write(data);
                    Bitmap bm = BitmapFactory.decodeFile(fileName);

                    Matrix matrix = new Matrix();
                    matrix.postRotate(180);
                    Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0,
                            bm.getWidth(), bm.getHeight(), matrix, false);
                    rotatedBitmap.compress(CompressFormat.JPEG, 100, fos);

                    fos.close();

                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }``

1 个答案:

答案 0 :(得分:0)

每当您使用新的CameraId

切换相机时,请调用此方法
public static void setCameraDisplayOrientation(Activity activity,
         int cameraId, android.hardware.Camera camera) {
     android.hardware.Camera.CameraInfo info =
             new android.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);
     int rotation = activity.getWindowManager().getDefaultDisplay()
             .getRotation();
     int degrees = 0;
     switch (rotation) {
         case Surface.ROTATION_0: degrees = 0; break;
         case Surface.ROTATION_90: degrees = 90; break;
         case Surface.ROTATION_180: degrees = 180; break;
         case Surface.ROTATION_270: degrees = 270; break;
     }

     int result;
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
         result = (info.orientation + degrees) % 360;
         result = (360 - result) % 360;  // compensate the mirror
     } else {  // back-facing
         result = (info.orientation - degrees + 360) % 360;
     }
     camera.setDisplayOrientation(result);
 }