android - 图像以错误的方向保存

标签 android camera commonsware

我正在使用这段代码:

https://github.com/commonsguy/cw-advandroid/blob/master/Camera/Picture/src/com/commonsware/android/picture/PictureDemo.java

Manifest 中,Activity Orientation 设置为 Landscape

所以,这就像只允许用户在横向模式下拍照,如果是在纵向模式下手持设备拍照,则保存的图像是这样的:

enter image description here

90 度旋转的图像。

在搜索解决方案后,我发现了这个:

Android - Camera preview is sideways

解决方案在哪里:

surfaceChanged() 中检查

Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
display.getRotation();

并相应地更改相机的显示方向

camera.setDisplayOrientation(90);

但是无论我旋转设备多少次,surfaceChanged() 都不会被调用。

我什至尝试删除 Manifest.xml 中的 orientation="Landscape",但随后预览本身显示为横向(可能是因为默认 android. view.SurfaceView 应该处于横向模式?)。

最佳答案

试试这个。

public void surfaceCreated(SurfaceHolder holder) {
    try {
        camera = Camera.open();
        camParam = camera.getParameters();
        Camera.Parameters params = camera.getParameters();
        String currentversion = android.os.Build.VERSION.SDK;
        Log.d("System out", "currentVersion " + currentversion);
        int currentInt = android.os.Build.VERSION.SDK_INT;
        Log.d("System out", "currentVersion " + currentInt);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            if (currentInt != 7) {
                camera.setDisplayOrientation(90);
            } else {
                Log.d("System out", "Portrait " + currentInt);

                params.setRotation(90);

                /*
                 * params.set("orientation", "portrait");
                 * params.set("rotation",90);
                 */
                camera.setParameters(params);
            }
        }
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            // camera.setDisplayOrientation(0);
            if (currentInt != 7) {
                camera.setDisplayOrientation(0);
            } else {
                Log.d("System out", "Landscape " + currentInt);
                params.set("orientation", "landscape");
                params.set("rotation", 90);
                camera.setParameters(params);
            }
        }
        camera.setPreviewDisplay(holder);
        camera.startPreview();
    } catch (IOException e) {
        Log.d("CAMERA", e.getMessage());
    }
}

关于android - 图像以错误的方向保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16098281/

相关文章:

android - 直接从 AlarmManager 唤醒服务

Android缩小相机图片的大小

android - 如何在Android中使用Spring RestTemplate在 Elasticsearch 中通过Json搜索

android - 如何使用 Gitlab CI/CD 生成签名发布 APK?

c# - 等待动画在 unity3d 中完成

matlab - 能同步Matlab的数据采集工具箱和图像采集工具箱吗?

java - 使用 SpannableString 长按 TextView 时出现 NullPointerException

android - 如何发送 FB 应用程序邀请,如 Kiwi App

ios - 使用前置摄像头进行手势检测

android - 开发报警应用