android - Camera API 拍摄的图像自动横向保存

标签 android bitmap camera rotation scale

我正在使用 blow post 来处理相机 API,并将相机翻转到后/前。 Fliping the Front Camera to Back Camera in Button Click using android

默认情况下,在三星设备中,它会横向打开相机预览,但

camera.setDisplayOrientation(90);

通过此代码,我能够以纵向方式打开相机预览。但是当我保存图像时,它会自动将其保存为横向。

通过使用这个方法

Matrix matrix = new Matrix();
    matrix.postRotate(90);
    Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
            bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(rotatedBitmap,
            curntViewWidth, curntViewHeight, true);

处理速度变慢。有没有人有任何其他方式可以使相机预览、捕获和保存图像方向相同..

我们有没有像这个应用一样可以打开相机并设置特定尺寸图像的库 https://play.google.com/store/apps/details?id=com.tinypiece.android.mlc

最佳答案

来自 Camera.Parameters#setRotation() 的文档:

If applications want to rotate the picture to match the orientation of what users see, apps should use OrientationEventListener and Camera.CameraInfo

 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);
 }

OrientationEventListener 是一个包含 onOrientationChanged() 的抽象类。此方法将从系统接收方向更改。这是一种可能的实现方式:

    OrientationEventListener listener = new OrientationEventListener(this,
            SensorManager.SENSOR_DELAY_NORMAL) {

        @Override
        public void onOrientationChanged(int i) {
           //Code from above goes here.
        }
    };
    listener.enable();

关于android - Camera API 拍摄的图像自动横向保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20844948/

相关文章:

android - Android Job 库 DailyJob 是否在 Application 类 onCreate 中启动?

android - Widevine 视频播放

java.lang. RuntimeException 未找到 Retrofit 注释。 (参数#3)

ios - 如何快速改变颜色的一小部分?

image-processing - Code Laboratories 的 DUO 3D 迷你传感器

c++ - ARtoolkit 加载从 4k 相机 Sony 捕获的视频文件

java - 显示空白图像并且 onItemClick 不起作用

android - 如何在 Espresso 测试中点击位图?

android - 从资源设置背景图片

android - 从android中的流位图创建视频