该文章转自网络

关于条形码扫描的软件,做软件需求时需要扫描时竖屏:       

android提供的SDK(android.hardware.Camera)里大概不能正常的使用竖屏(portrait layout)加载照相机,当用竖屏模式加载照相机时会产生以下情况:

1. 照相机成像左倾90度(倾斜);

2. 照相机成像长宽比例不对(失比)。

基本上解决办法如下:

1、在AndroidManifest.xml里面配置一下 ,使CaptureActivity属性为portrait:   android:screenOrientation="portrait"

2、如果只是单纯的想改变照相机成像的方向,只需要在包com.google.zxing.client.android.camera下的   CameraConfigurationManager类中增加方法   

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) {         }       

}    

然后在方法void setDesiredCameraParameters(Camera camera){}中调用, setDisplayOrientation(camera, 90);     具体位置在camera.setParameters(parameters);语句前面。

3、改变完方向你会发现方向改变了可是分辨率会变得很低,接下来就是优化了  

1)首先在类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;   

(2)然后是在DecodeHandler类中的方法private void decode(byte[] data,int width,int 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];   

(3)再就是CameraConfigurationManager类中的方法void initFromCameraParameters(Camera camera){}中添加如下代码:

Point screenResolutionForCamera = new Point();    

screenResolutionForCamera.x = screenResolution.x;    

screenResolutionForCamera.y = screenResolution.y;    

// preview size is always something like 480*320, other 320*480 if (screenResolution.x < screenResolution.y) {   screenResolutionForCamera.x = screenResolution.y;   screenResolutionForCamera.y = screenResolution.x; 注: 到这就完成了。如果还有问题,只需下载资源文件直接替换相应文件即可;

arrow
arrow
    文章標籤
    androids
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()