Android开源二维码识别项目zxing横屏改为竖屏识别解决方案

在网上找了很多方法,但最后都有问题,自己调试了好几个小时,最后终于完美解决了竖屏识别。

首先你需要有zxing项目的简化版代码。

使用简化版可以免去许多不必要的代码,方便学习研究,更好定位核心功能。

如果你调试成功后,就可以着手修改将其变为竖屏识别了。

第1步:

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

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 });       
i f  (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);句后面添加如下代码,这段代码是为了解决摄像头竖过来后图像拉伸的问题:

//为竖屏添加    
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步:

PlanarYUVLuminanceSource类中的getRow()方法为识别条形码部分,

getMatrix()方法为识别二维码部分

renderCroppedGreyscaleBitmap()方法为生成获取的码图部分

将getRow()中的:

int offset = (y + top) * dataWidth + left;

getMatrix()中的:

int inputOffset = top * dataWidth + left;
inputOffset += dataWidth;

renderCroppedGreyscaleBitmap()中的:

int inputOffset = top * dataWidth + left;
inputOffset += dataWidth;

这些语句中dataWidth全部替换为dataHeight

同时将PlanarYUVLuminanceSource构造方法中:

if  (left + width > dataWidth || top + height > dataHeight) {       throw   new  IllegalArgumentException("Crop rectangle does not fit within image data.");    }

dataWidth与dateHeight中互换位置即可。

此时,你的程序竖屏识别码图应该没有任何问题了。至于取景框的样式,大家可以在自定义的ViewfinderView中修改成自己喜欢的样式。






http://407827531.iteye.com/blog/1488676

解决方法:

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

2.在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;

3.在CameraConfigurationManager.java中,在setDesiredCameraParameters方法中添加一句
  camera.setDisplayOrientation(90);

4.在AndroidManifest.xml中,把Activity的属性android:screenOrientation="landscape"
改为
  android:screenOrientation="portrait"

编译运行即可!


参考:

http://code.google.com/p/zxing/issues/detail?id=178#c46


代码:
https://github.com/pplante/zxing-android



zxing横屏改为竖屏识别,多次扫描,以及存在的摄像拉伸的问题

摘自http://www.cnblogs.com/moka/archive/2013/05/24/3096937.html

按照以上做法,可以实现。

使用简化版可以免去许多不必要的代码,方便学习研究,更好定位核心功能。

如果你调试成功后,就可以着手修改将其变为竖屏识别了。

第1步:

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

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);句后面添加如下代码,这段代码是为了解决摄像头竖过来后图像拉伸的问题:

//为竖屏添加
    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步:

PlanarYUVLuminanceSource类中的getRow()方法为识别条形码部分,

getMatrix()方法为识别二维码部分

renderCroppedGreyscaleBitmap()方法为生成获取的码图部分

将getRow()中的:

int offset = (y + top) * dataWidth + left;

getMatrix()中的:

int inputOffset = top * dataWidth + left;
inputOffset += dataWidth;

renderCroppedGreyscaleBitmap()中的:

int inputOffset = top * dataWidth + left;
inputOffset += dataWidth;

这些语句中dataWidth全部替换为dataHeight

同时将PlanarYUVLuminanceSource构造方法中:

if (left + width > dataWidth || top + height > dataHeight) {
      throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
    }

dataWidth与dateHeight中互换位置即可。

此时,你的程序竖屏识别码图应该没有任何问题了。至于取景框的样式,大家可以在自定义的ViewfinderView中修改成自己喜欢的样式。


——————————————————————————————

测试,可以竖屏,但是取景还是得横着来。

http://blog.csdn.net/sunmanzth/article/details/6860157  转摘这篇

解决方法:

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

2.在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;

3.在CameraConfigurationManager.java中,在setDesiredCameraParameters方法中添加一句
  camera.setDisplayOrientation(90);

4.在AndroidManifest.xml中,把Activity的属性android:screenOrientation="landscape"
改为
  android:screenOrientation="portrait"

编译运行即可!


参考:

http://code.google.com/p/zxing/issues/detail?id=178#c46


代码:

https://github.com/pplante/zxing-android

————————

按照下篇的解决方案,成功解决问题。

实现zxing多次扫描问题:

private void continuePreview()

 {
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
        initCamera(surfaceHolder);
        if (handler != null)
            handler.restartPreviewAndDecode();   
 }

CaptureActivityHandler中restartPreviewAndDecode属性由private 设置成public

——————————————————————————————

扫描二维码图片时候,正方形的二维码图片,显示会发现变拉长、拉伸。

找着的解决途径如下:

http://www.apkbus.com/android-94078-1-1.html

更改CameraConfigurationManager.java文件

在 Log.d(TAG, "Screen resolution: " + screenResolution);这句之后增加
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;
        }
再更改cameraResolution = getCameraResolution(parameters, screenResolution);为cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);

PS:有时间一定要读zxing源码的。


你可能感兴趣的:(Android开发)