1. 程式人生 > >android zxing二維碼橫屏改豎屏

android zxing二維碼橫屏改豎屏

1.修改manifest檔案,將CaptureActivity的screenOrentatino設為portrait。

2.攝像頭調整為豎向在CameraConfigurationManager類中新增如下方法:

protected void setDisplayOrientation(Camera camera, int angle) {//mycode

      Method downPolymorphic;       

       try { 
           
           downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
     
           if (downPolymorphic != null)
               
               downPolymorphic.invoke(camera, new Object[] { angle });       
     
       } catch (Exception e1) {   
           e1.printStackTrace();
       }      
}
在CameraConfigurationManager的setDesiredCameraParameters方法中呼叫此方法。

setDisplayOrientation(camera, 90);//mycode

3.CameraConfigurationManager類的initFromCameraParameters方法中註釋一下程式碼:

//這段程式碼用於橫屏的時候+
    /*if (width < height) {
      Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");
      int temp = width;
      width = height;
      height = temp;
    }*/

4.CameraManager類的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;*/
      
      //豎屏模式mycode
      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;
5.CameraManager類的getFramingRect方法修改:
//用於橫屏時候的掃描mycode
      //int width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
      //int height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
      
      //用於豎屏的掃描
      int height = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
      int width = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);

6.DecodeHandler修改decode方法

PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);之前加入一下程式碼
//mycode
    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;
    data = rotatedData;
    
    PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);