天天看點

ZXing二維碼掃描橫屏變豎屏,并解決攝像拉伸問題

第1步:

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

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

第2步:

我們要把攝像頭預覽景調為豎向

CameraConfigurationManager類中的setDesiredCameraParameters()方法中添加如下代碼:

// 使攝像頭旋轉90度
setDisplayOrientation(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。

上一篇: 媽媽