摄像头camera 旋转90度 解决方法

拍照需要竖屏时的解决方法。
zxing官方wiki上面的解决办法。

基本思路如下。
There are 4 relative files:
1.manifest.xml, you need to make CaptureActivity portrait.

2.DecodeHandler.java, rotate data before buildLuminanceSource, it works becuase in YCbCr_420_SP and YCbCr_422_SP, the Y channel is planar and appears first

  1. byte[] rotatedData = new byte[data.length]; 
  2. for (int y = 0; y < height; y++) { 
  3.      for (int x = 0; x < width; x++) 
  4.          rotatedData[x * height + height - y - 1] = data[x + y * width]; 
  5.  }

3.CameraManager.java, getFramingRectInPreview() need to be modified.

  1.  rect.left = rect.left * cameraResolution.y / screenResolution.x;
  2.  rect.right = rect.right * cameraResolution.y / screenResolution.x;
  3.  rect.top = rect.top * cameraResolution.x / screenResolution.y;
  4.  rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;

4.CameraConfigurationManager.java, set camera orientation to portrait in setDesiredCameraParameters() use

  1.  parameters.set("orientation", "portrait");

注:版本兼容请看下面。
and in getCameraResolution(), you need to swap x and y, because camera preview size is something like 480*320, other than 320*480.

  1.  int tmp = cameraResolution.x;
  2.  cameraResolution.x = cameraResolution.y;
  3.  cameraResolution.y = tmp;
  4.  return cameraResolution;

说明:
关于摄像头旋转90度的时候,不同的sdk版本方法不同。
兼容方法如下

  1.  if (Integer.parseInt(Build.VERSION.SDK) >= <IMG class=wp-smiley alt=8) src="http://www.andcoder.com/wp-includes/images/smilies/icon_cool.gif">
  2.     setDisplayOrientation(camera, 90);
  3.    else {
  4.     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  5.      parameters.set("orientation", "portrait");
  6.      parameters.set("rotation", 90);
  7.     }
  8.     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  9.      parameters.set("orientation", "landscape");
  10.      parameters.set("rotation", 90);
  11.     }
  12.  }
  13.  protected void setDisplayOrientation(Camera camera, int angle) {
  14.    Method downPolymorphic;
  15.    try {
  16.     downPolymorphic = camera.getClass().getMethod(
  17.       "setDisplayOrientation", new Class[] { int.class });
  18.     if (downPolymorphic != null)
  19.      downPolymorphic.invoke(camera, new Object[] { angle });
  20.    } catch (Exception e1) {
  21.    }
  22.   }

转自:http://www.andcoder.com/archives-2011-10-383.html

大意:

1.在DecodeHandler.java中,修改decode方法
  PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);

    byte[] rotatedData = new byte[data.length];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++)
            rotatedData[x * height + height - y - 1] = data[x + y * width];
    }
    int tmp = width; // Here we are swapping, that's the difference to #11
    width = height;
    height = tmp;
    
    PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);

2.在CameraManager.java中,注释代码:
            // rect.left = rect.left * cameraResolution.x / screenResolution.x;
            // rect.right = rect.right * cameraResolution.x / screenResolution.x;
            // rect.top = rect.top * cameraResolution.y / screenResolution.y;
            // rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
修改为
            rect.left = rect.left * cameraResolution.y / screenResolution.x;
            rect.right = rect.right * cameraResolution.y / screenResolution.x;
            rect.top = rect.top * cameraResolution.x / screenResolution.y;
            rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;

3.在CameraConfigurationManager.java中,在setDesiredCameraParameters方法中添加一句
  camera.setDisplayOrientation(90);

4.在AndroidManifest.xml中,把Activity的属性android:screenOrientation="landscape"
改为
  android:screenOrientation="portrait"

编译运行即可!


参考:

http://code.google.com/p/zxing/issues/detail?id=178#c46


代码:

https://github.com/pplante/zxing-android

---------------------------------华丽分割线-----------------------------------

在写相机相关应用的时候遇到捕获的画面方向和手机的方向不一致的问题,比如手机是竖着拿的,但是画面是横的,这是由于摄像头默认捕获的画面byte[]是根据横向来的,而你的应用是竖向的,解决办法是调用setDisplayOrientation来设置PreviewDisplay的方向,效果就是将捕获的画面旋转多少度显示。
设置 preview 的顺时针旋转角度。这将影响 preview frames 和拍照之后的相片显示。该方法主要用于垂直模式的应用。注意在旋转之前, front-facing cameras 的 preview 显示是水平 flip 的,这就是说, image 是沿着 camera sensor 的垂直中心轴来反射的。所以用户可以像照镜子一样看到他们自己。这不会影响传入函数 onPreviewFrame(byte[], Camera) 的、 JPEG 相片的、或记录的 video 的 byte array 的顺序,你可以自己做旋转处理。在 preview 期间是不允许调用该方法的。如果你想要是你的照片和显示出来的角度一致,你可以参考下列代码:

public static void setCameraDisplayOrientation ( Activity activity ,
          int cameraId , android.hardware.Camera camera ) {
     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 ;
      }
     camera.setDisplayOrientation ( result );
  }

来源:   http://www.fish24k.com/?p=655965</!>

--------------------------------又来-华丽分割线-----------------------------------

android 相机旋转90度的处理方法 

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

  Camera.Parameters parameters = camera.getParameters();

//SDK版本选择,兼容

  if (Integer.parseInt(Build.VERSION.SDK) >= 8)
   setDisplayOrientation(camera, 90);
  else {
   if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    parameters.set("orientation", "portrait");
    parameters.set("rotation", 90);
   }
   if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    parameters.set("orientation", "landscape");
    parameters.set("rotation", 90);
   }
  }

  parameters.setPictureFormat(PixelFormat.JPEG);
  List<Size> sizes = parameters.getSupportedPreviewSizes();
  Size optimalSize = getOptimalPreviewSize(sizes, width, height);
  parameters.setPictureSize(optimalSize.width, optimalSize.height);

  camera.setParameters(parameters);
  camera.startPreview();
 }

// 
 protected void setDisplayOrientation(Camera camera, int angle) {
  Method downPolymorphic;
  try {
   downPolymorphic = camera.getClass().getMethod(
     "setDisplayOrientation", new Class[] { int.class });
   if (downPolymorphic != null)
    downPolymorphic.invoke(camera, new Object[] { angle });
  } catch (Exception e1) {
  }
 

// 获得 camera 大小
  private Size getOptimalPreviewSize(List<Size> sizes, int w, int h)
  {        
   final double ASPECT_TOLERANCE = 0.2;       
   double targetRatio = (double) w / h;        
   if (sizes == null)            
    return null;         
   Size optimalSize = null;        
   double minDiff = Double.MAX_VALUE;         
   int targetHeight = h;         
   // Try to find an size match aspect ratio and size        
   for (Size size : sizes)
   {            
    Log.d("dd", "Checking size " + size.width + "w " + size.height + "h");           
    double ratio = (double) size.width / size.height;           
    if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)               
     continue;            
    if (Math.abs(size.height - targetHeight) < minDiff)
    {                
     optimalSize = size;                
     minDiff = Math.abs(size.height - targetHeight);            
     }        
    }         
   // Cannot find the one match the aspect ratio, ignore the        
   // requirement        
   if (optimalSize == null)
   {
    minDiff = Double.MAX_VALUE;            
    for (Size size : sizes) {
     if (Math.abs(size.height - targetHeight) < minDiff)
     {
      optimalSize = size;
      minDiff = Math.abs(size.height - targetHeight);
     }
    }
   }
   return optimalSize;    
  }

http://blog.163.com/hongwei_benbear/blog/static/1183952912011797180426/

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
rk3568是一款基于ARM Cortex-A55架构的64位高性能嵌入式处理器,属于RK系列中的一员。它支持Android 11操作系统和多种外设接口,包括摄像头接口。然而,在使用rk3568处理器时,摄像头默认旋转方向具体是怎么确定的呢? 首先,需要明确的是,RK3568处理器主要由四个核心部分组成,包括CPU、GPU、ISP和VPU。其中,ISP(Image Signal Processor)是用于处理相机数据的。一般情况下,系统会在启动时读取ISP设置,并将其应用到整个系统中。 在Android 11系统上,系统开发人员可以通过修改源代码来确定RK3568处理器摄像头默认旋转方向。源代码中有一个名为“android.hardware.camera2”的API,它提供了与相机相关的所有设置。其中,“android.hardware.camera2.CameraCharacteristics”类可以查询相机支持的所有特性。“android.hardware.camera2.CaptureRequest”类可以设置相机参数,例如曝光时间、ISO、焦距、闪光灯等。 通过这些API,开发人员可以获取和修改摄像头旋转方向设置。例如,在设置曝光时间时,可以将“CaptureRequest.JPEG_ORIENTATION”参数设置为90或270,这将会使得相机拍摄的照片或视频自动旋转90或270。这样就可以根据具体需求来设置摄像头默认旋转方向,以达到最佳效果。 综上所述,rk3568处理器摄像头默认旋转方向可以通过修改源代码来确定,通过API可以查询摄像头支持的所有特性、设置相机参数等。在使用rk3568处理器时,开发人员应该仔细阅读官方文档,并根据具体需求来对相机参数进行调整,以达到最佳效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值