图像保存方向错误 [英] Image saved with wrong orientation

查看:162
本文介绍了图像保存方向错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码:



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



Manifest 方向设置为横向



因此,它只允许用户在横向模式下拍摄照片,如果图片是通过将设备保持在纵向模式下拍摄的,则保存的图片如下所示:





90度旋转的图片



搜索解决方案后,我发现:





其中的解决方案是:在 surfaceChanged()中检查





 显示display =((WindowManager)getSystemService(WINDOW_SERVICE))。getDefaultDisplay(); 
display.getRotation();

并相应地更改 Camera的displayOrientation

  camera.setDisplayOrientation(90); 

但是无论我旋转设备多少次, surfaceChanged / code>从未被调用。



我甚至尝试删除 orientation = strong> Manifest.xml ,但预览本身显示为横向(可能是因为默认 android.view.SurfaceView 应该在 Landscape



<$>

p $ p> 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());
}
}


I am using this code:

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

where in Manifest, Activity Orientation is set to Landscape.

So, its like allowing user to take picture only in Landscape mode, and if the picture is taking by holding the device in portrait mode, the image saved is like this:

a 90 degree rotated image.

After searching for a solution, I found this:

Android - Camera preview is sideways

where the solution is:

in surfaceChanged() check for

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

and change the Camera's displayOrientation accordingly.

camera.setDisplayOrientation(90);

But no matter how many times I rotate the device, surfaceChanged() never gets called.

I even tried removing orientation="Landscape" in the Manifest.xml, but then the preview itself is shown sideways(may be because default android.view.SurfaceView is supposed to be in Landscape mode?).

解决方案

Try this.

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

这篇关于图像保存方向错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆