Android相机如何在SurfaceView上以竖屏模式显示?

38

我尝试了几种方法来使相机预览在SurfaceView中以竖屏显示,但都不起作用。我在一台安装了2.0.1版本的Droid上进行测试。我尝试了以下方法:

1)通过强制布局为竖屏:this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

2)使用

Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
parameters.setRotation(90);
camera.setParameters(parameters);

我还有其他尝试的方法吗?如果这是Android或手机中的错误,如何确保这是正确的,以便我有证据通知客户呢?

谢谢, Prasanna


请查看 - https://dev59.com/g2LVa4cB1Zd3GeqP1fYj#10259572 - Suvam Roy
有人可以帮忙吗?https://dev59.com/cofca4cB1Zd3GeqPlaPJ - user4050065
6个回答

65

从API lvl 8版本开始,可以使用以下方法:

public final void setDisplayOrientation (int degrees)

例如,在清单文件中设置为竖屏:

public void surfaceCreated(SurfaceHolder holder) {
    mCamera = Camera.open();
    mCamera.setDisplayOrientation(90);

13
我已经做了这个,但我得到的字节数组仍然给我提供了一张横向照片。 - tasomaniac
3
为什么在使用 mCamera.setDisplayOrientation(90) 方法设置摄像头预览时,预览画面可以旋转 90 度,但录制的视频却不能呢?录制出来的视频与预览画面不一样。 - mum
嘿,你可以使用param.setRotation(..)方法来实现这个。我还在努力弄清楚它的工作原理。它真是太棒了! - sunlover3

12
我有一个在2.1版本(Desire测试过)中可用的竖屏模式解决方案。
活动页面的屏幕方向设置为纵向。(android:screenOrientation="portrait")
摄像头参数:
Camera.Parameters p = mCamera.getParameters();
 p.set("jpeg-quality", 100);
 p.set("orientation", "landscape");
 p.set("rotation", 90);
 p.setPictureFormat(PixelFormat.JPEG);
 p.setPreviewSize(h, w);// here w h are reversed
 mCamera.setParameters(p);

并且图像将是纵向。

您用于相机的SurfaceHolder必须与手机预览大小兼容,通常是屏幕分辨率。

在Desire 2.2上有趣功能无法正常工作... 这是修复方法:

   At surfaceCreated(..) or when you have this line
camera = Camera.open();
       add
camera.setDisplayOrientation(90);//only 2.2>

Camera.Parameters p = camera.getParameters();
    p.set("jpeg-quality", 100);
p.setRotation(90);
p.setPictureFormat(PixelFormat.JPEG);
p.setPreviewSize(h, w);
camera.setParameters(p);

10

你可以尝试这个方法(适用于2.2或以下的版本)。在将图像保存到SD卡之前,我会将其旋转。但仅适用于纵向模式。如果你需要为两种模式都制作,则应检查相机方向并在捕获图像之前进行一些检查。

PictureCallback jpegCallback = new PictureCallback() {
   public void onPictureTaken(byte[] data, Camera camera) {
      FileOutputStream outStream = null;
      try {
        imageFilePath = getFilename();
        InputStream is = new ByteArrayInputStream(data);
        Bitmap bmp = BitmapFactory.decodeStream(is);
        // Getting width & height of the given image.
        if (bmp != null){
           int w = bmp.getWidth();
           int h = bmp.getHeight();
           // Setting post rotate to 90
           Matrix mtx = new Matrix();
           mtx.postRotate(90);
           // Rotating Bitmap
           Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
           ByteArrayOutputStream stream = new ByteArrayOutputStream();
           rotatedBMP.compress(Bitmap.CompressFormat.PNG, 100, stream);
           byte[] byteArray = stream.toByteArray(); 
           outStream = new FileOutputStream
                              (String.format(imageFilePath,System.currentTimeMillis()));
           outStream.write(byteArray);
           outStream.close();
        } else {
           outStream = new FileOutputStream
                              (String.format(imageFilePath,System.currentTimeMillis()));
           outStream.write(data);
           outStream.close();           
        }       

        preview.camera.startPreview();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
  }
};

2

0

在你需要明确设置方向之前,没有必要设置任何方向参数。默认情况下,它支持此功能。在我的情况下,我有一个Activity,在该Activity上面我有一个相机视图,因此我没有为相机属性设置任何方向,而是在Manifest文件中将Activity的方向设置为纵向。现在应用程序看起来和工作得很好。可能对某些人有帮助。

谢谢。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接