ZXing二维码扫描横屏变竖屏,并解决摄像拉伸问题

第1步:

在AndroidManifest中将CaptureActivity的screenOrientation属性做如下修改:


android:name=".zxing.activity.CaptureActivity"
          android:screenOrientation="portrait"/>


第2步:

我们要把摄像头预览景调为竖向

CameraConfigurationManager类中的setDesiredCameraParameters()方法中添加如下代码:

// 使摄像头旋转90setDisplayOrientation(camera, 90);
然后在CameraConfigurationManager类添加setDisplayOrientation()方法:

/*改变照相机成像的方向的方法*/
protected void setDisplayOrientation(Camera camera, int angle) {
  Method downPolymorphic = null;
  try {
    downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
    if (downPolymorphic != null)
      downPolymorphic.invoke(camera, new Object[]{angle});
  } catch (NoSuchMethodException e) {
    e.printStackTrace();
  } catch (IllegalArgumentException e) {
    e.printStackTrace();
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  } catch (InvocationTargetException e) {
    e.printStackTrace();
  }
}
这时候摄像会有一个拉伸的现象,特别在将手机竖屏边横屏时感觉最深,可如下操作解决摄像头竖过来后图像拉伸的问题

在CameraConfigurationManager中的initFromCameraParameters()方法的Log.d(TAG, "Screen resolution: " + screenResolution);句后面添加如下代码:

//    cameraResolution = getCameraResolution(parameters, screenResolution);
//    Log.d(TAG, "Camera resolution: " + screenResolution);
    //为竖屏添加
    Point screenResolutionForCamera = new Point();
    screenResolutionForCamera.x = screenResolution.x;
    screenResolutionForCamera.y = screenResolution.y;
    if (screenResolution.x < screenResolution.y) {
      screenResolutionForCamera.x = screenResolution.y;
      screenResolutionForCamera.y = screenResolution.x;
    }
    // 下句第二参数要根据竖屏修改
    cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);


第3步:

CameranManager类中getFramingRectInPreview()方法作如下改变:

//      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;


第4步:

在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);


第5步:

在CameraConfigurationManager.java中,在setDesiredCameraParameters方法中添加:

camera.setDisplayOrientation(90);


到此完成!

很多人说还要将PlanarYUVLuminanceSource类中dataWidth全部替换为dataHeight,但亲自测试并不需要如此做,因为横屏变竖屏,本来就更改了width和height。

你可能感兴趣的:(Android,zxing,二维码,摄像拉伸,横屏变竖屏)