【问题标题】:how to rotate Image captured using takePicture?如何旋转使用 takePicture 捕获的图像?
【发布时间】:2015-01-12 11:48:15
【问题描述】:

我准备的视图(使用 setDisplayOrientation())处于纵向模式,但生成的图像(使用 takePicture() 拍摄,使用 SaveImageTask 保存)处于横向模式。

这是我的预览和捕获代码:

public void startPreview() {
    this.mCamera.setDisplayOrientation(90);
    try {
        this.mCamera.setPreviewDisplay(this.mSurfaceHolder);
        this.mCamera.startPreview();
     } catch (Exception e) {
        Log.d("camera" , "in startPreview Catch");
     }
}

public void captureNow(View view) {
    this.mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
    this.startPreview();
}

这是我捕获的图像保存代码:

public class SaveImageTask extends AsyncTask<byte[], Void, Void> {
    @Override
    protected Void doInBackground(byte[]... data) {
        FileOutputStream outStream = null;
        // Write to SD Card
        try {
            File dir = new File(
            Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "camTest");
            dir.mkdirs();
            String fileName = String.format("%d.jpg", System.currentTimeMillis());
            File outFile = new File(dir, fileName);
            outStream = new FileOutputStream(outFile);
            outStream.write(data[0]);
            outStream.flush();
            outStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
        return null;
    }
}

如何旋转捕获的图像?传感器似乎始终处于横向模式。(注:API 21)

【问题讨论】:

    标签: android android-camera


    【解决方案1】:

    我正在发布我的部分代码,可能会对您有所帮助,谢谢

            File f = new File(path + "image" + imagecount + ".jpg");
            ExifInterface exif = new ExifInterface(f.getPath());
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
    
            int angle = 0;
    
            if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                angle = 90;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                angle = 180;
            } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                angle = 270;
            }
    
            Matrix mat = new Matrix();
            mat.postRotate(angle);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 3;
    
            Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
                    null, options);
            correctBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
                    bmp.getHeight(), mat, true);
            ByteArrayOutputStream outstudentstreamOutputStream =                           new ByteArrayOutputStream();
            correctBmp.compress(Bitmap.CompressFormat.PNG, 100,
                    outstudentstreamOutputStream);
            capturedImageIV.setImageBitmap(correctBmp);
        } catch (IOException e) {
            Log.w("TAG", "-- Error in setting image");
        } catch (OutOfMemoryError oom) {
            Log.w("TAG", "-- OOM Error in setting image");
        }
    

    【讨论】:

      【解决方案2】:

      如果你想在拍照前改变方向,你必须先配置相机。

      public void onOrientationChanged(int orientation) {
         if (orientation == ORIENTATION_UNKNOWN) return;
         android.hardware.Camera.CameraInfo info =
              new android.hardware.Camera.CameraInfo();
         android.hardware.Camera.getCameraInfo(cameraId, info);
         orientation = (orientation + 45) / 90 * 90;
         int rotation = 0;
         if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
           rotation = (info.orientation - orientation + 360) % 360;
         } else {  // back-facing camera
           rotation = (info.orientation + orientation) % 360;
         }
         mParameters.setRotation(rotation);
      }
      

      AFAIK,您不能在写入光盘之前更改 exif 数据。所以,如果你以后想改变方向,你可以按照 Arun Antoney 的方式来做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-11
        • 1970-01-01
        • 2020-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多