二维码摄像横竖屏切换

来源:互联网 发布:车载电子狗软件 编辑:程序博客网 时间:2024/05/21 19:41

转载:http://blog.csdn.net/zhouli_csdn/article/details/49930323


1.修改manifest文件,将CaptureActivity的screenOrentatino设为portrait。

2.摄像头调整为竖向在CameraConfigurationManager类中添加如下方法:

[html] view plain copy
 print?
  1. protected void setDisplayOrientation(Camera camera, int angle) {//mycode  
  2.   
  3.       Method downPolymorphic;         
  4.   
  5.        try {   
  6.              
  7.            downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });  
  8.        
  9.            if (downPolymorphic != null)  
  10.                  
  11.                downPolymorphic.invoke(camera, new Object[] { angle });         
  12.        
  13.        } catch (Exception e1) {     
  14.            e1.printStackTrace();  
  15.        }        
  16. }  
在CameraConfigurationManager的setDesiredCameraParameters方法中调用此方法。

setDisplayOrientation(camera, 90);//mycode


3.CameraConfigurationManager类的initFromCameraParameters方法中注释一下代码:

[html] view plain copy
 print?
  1. //这段代码用于横屏的时候+  
  2.     /*if (width < height) {  
  3.       Log.i(TAG, "Display reports portrait orientation; assuming this is incorrect");  
  4.       int temp = width;  
  5.       width = height;  
  6.       height = temp;  
  7.     }*/  

4.CameraManager类的getFramingRectInPreview修改如下代码;

[html] view plain copy
 print?
  1. //横屏模式  
  2.       /*rect.left = rect.left * cameraResolution.x / screenResolution.x;  
  3.       rect.right = rect.right * cameraResolution.x / screenResolution.x;  
  4.       rect.top = rect.top * cameraResolution.y / screenResolution.y;  
  5.       rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;*/  
  6.         
  7.       //竖屏模式mycode  
  8.       rect.left = rect.left * cameraResolution.y / screenResolution.x;       
  9.       rect.right = rect.right * cameraResolution.y / screenResolution.x;       
  10.       rect.top = rect.top * cameraResolution.x / screenResolution.y;       
  11.       rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;  
5.CameraManager类的getFramingRect方法修改:
[html] view plain copy
 print?
  1. //用于横屏时候的扫描mycode  
  2.       //int width = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);  
  3.       //int height = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);  
  4.         
  5.       //用于竖屏的扫描  
  6.       int height = findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);  
  7.       int width = findDesiredDimensionInRange(screenResolution.y, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);  

6.DecodeHandler修改decode方法

[html] view plain copy
 print?
  1. PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);之前加入一下代码  

[html] view plain copy
 print?
  1. //mycode  
  2.     byte[] rotatedData = new byte[data.length];  
  3.     for (int y = 0; y < height; y++) {  
  4.         for (int x = 0; x < width; x++)  
  5.         rotatedData[x * height + height - y - 1] = data[x + y * width];  
  6.     }  
  7.     int tmp = width; // Here we are swapping, that's the difference to #11  
  8.     width = height;  
  9.     height = tmp;  
  10.     data = rotatedData;  
  11.       
  12.     PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);  

0 0
原创粉丝点击