【问题标题】:Android Camera Preview RotationAndroid 相机预览旋转
【发布时间】:2012-04-30 14:11:37
【问题描述】:

根据 Android 开发者网站:

android 2.2之后有这个功能

“设置显示方向”

调整相机预览旋转。

另外,根据 Android 开发者网站,我们可以找到以下源代码。

android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0 ;
    switch ( rotation ) {
        case Surface.ROTATION_0 : degrees = 0 ; break ;
        case Surface.ROTATION_90 : degrees = 90 ; break ;
        case Surface.ROTATION_180 : degrees = 180 ; break ;
        case Surface.ROTATION_270 : degrees = 270 ; break ;
    }
    int result ;
    if ( info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = ( info.orientation + degrees ) % 360 ;
         result = ( 360 - result ) % 360 ;   // compensate the mirror
    } else {   // back-facing

        result = ( info.orientation - degrees + 360 ) % 360 ;

    }

但是,我无法使用某种设备。 如三星 Galaxy Y S5360、S5660、YP-G1、YP-G70 等

只是部分机器不工作,Galaxy Nexus、SII 或一些高端设备,它工作正常。

setDisplayOrientation 不支持,或者设备固件没有准备好?

PS。所有设备均为 Android 2.3.1 或更高版本。

帮助。

【问题讨论】:

  • 我在 Galaxy Y 上遇到了同样的问题。你发现了什么吗?我至少想知道 setDisplayOrientation 是否成功,如果没有成功则将其锁定为横向。
  • 终于,没有解决。只需将相机表面活动设置为水平视图即可。
  • 有没有人列出发生此错误的设备?任何帮助将不胜感激。

标签: android orientation android-camera


【解决方案1】:

设置相机方向显示的问题不适用于这些设备的操作系统版本,基本上是(姜饼),但对于上面的设备,它可以正常工作。

我也在这些设备上尝试过,横向模式可以正常工作,但纵向模式有问题。如果要强制纵向,只需将相机方向强制为 90 度 通过这个在

public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {

camera.setDisplayOrientation(90);


}

以及在显示或保存之前旋转图片 在

    PictureCallback myPictureCallback_JPG = new PictureCallback(){


     public void onPictureTaken(byte[] arg0, Camera arg1) {

    Bitmap bitmapPicture= BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
      Matrix matrix = new Matrix();
     matrix.postRotate(90);
     int height=bitmapPicture.getHeight();
     int width=bitmapPicture.getWidth();
     Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapPicture,height,width, true);
      Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
}

希望这会有所帮助

【讨论】:

    【解决方案2】:

    尝试将相机视图的 xml 的方向设置为横向。

    【讨论】:

    • 如果 OP 不想横向,他不会问如何首先获得纵向,这不是一个真正有用的答案。
    • @Shirkrin 答案首先看起来很奇怪。但事实是,在某些设备中,相机的预览固定为横向。如果您将相机预览的 xml 调整为横向,它将适合所有设备。接下来你要做的就是使用矩阵来旋转相机拍摄的照片。
    猜你喜欢
    • 1970-01-01
    • 2013-01-26
    • 2012-11-09
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多