【问题标题】:images are saved upside down from front facing android camera图像从前置 android 摄像头倒置保存
【发布时间】:2015-04-24 09:11:09
【问题描述】:

我正在尝试从我的前置摄像头捕获图像并将其保存到我的画廊中,它可以在纵向和横向两个方向上与后置摄像头一起正常工作 但是对于前置摄像头,它仅适用于风景,

如果是 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();
                }``

【问题讨论】:

  • but nothing works。请更好地解释发生了什么。 fos = new FileOutputStream(fileName);。此时该文件被删除并设置为大小 0。fos.write(data); 这里发生了什么? Bitmap bm = BitmapFactory.decodeFile(fileName);。你在这里加载那个空文件。更好:把Bitmap bm = BitmapFactory.decodeFile(fileName); as first statement.
  • 我现在看到,也许您首先尝试将数据中的位图保存到文件中,然后将文件加载为位图,旋转它并再次保存。那么这个方法只有在你放一个 fos.close(); 时才能工作。在使用 decodeFile() 之前。然后在旋转之后你应该创建一个新的 FileOutputStream。但是为什么不在保存前旋转位图呢?

标签: android camera


【解决方案1】:

每当您使用新的 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);
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    相关资源
    最近更新 更多