二维码扫描匡大小、扫描旋转。扫描拉伸问题解决

  1.     扫描框大小的调整:

private static final int MIN_FRAME_WIDTH = 240;

private static final int MIN_FRAME_HEIGHT = 240;
private static final int MAX_FRAME_WIDTH = 480;

private static final int MAX_FRAME_HEIGHT = 360;

            // 扫描匡的固定大小
/*
* Point screenResolution = configManager.getScreenResolution(); if
* (framingRect == null) { if (camera == null) { return null; } int
* width = screenResolution.x * 3 / 4; if (width < MIN_FRAME_WIDTH) {
* width = MIN_FRAME_WIDTH; } else if (width > MAX_FRAME_WIDTH) { width
* = MAX_FRAME_WIDTH; } int height = screenResolution.y * 3 / 4; if
* (height < MIN_FRAME_HEIGHT) { height = MIN_FRAME_HEIGHT; } else if
* (height > MAX_FRAME_HEIGHT) { height = MAX_FRAME_HEIGHT; } int
* leftOffset = (screenResolution.x - width) / 2; int topOffset =
* (screenResolution.y - height) / 2; framingRect = new Rect(leftOffset,
* topOffset, leftOffset + width, topOffset + height); Log.d(TAG,
* "Calculated framing rect: " + framingRect); } return framingRect;
*/

  改成:

          // 根据实际设备屏幕大小而定
Point screenResolution = configManager.getScreenResolution();
if (framingRect == null) {
if (camera == null) {
return null;
}
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = (int) (metrics.widthPixels * 0.6);
int height = (int) (width * 0.9);
int leftOffset = (screenResolution.x - width) / 2;
int topOffset = (screenResolution.y - height) / 2;
framingRect = new Rect(leftOffset, topOffset, leftOffset + width,
topOffset + height);
Log.d(TAG, "Calculated framing rect: " + framingRect);
}
return framingRect;



2.图像旋转解决:


第一步:
在AndroidManifest中将CaptureActivity的screenOrientation属性做如下修改:
android:screenOrientation="portrait" 


第二步:


首先,CameraConfigurationManager类中的setDesiredCameraParameters()方法中添加如下代码:
// 使摄像头旋转90度    
setDisplayOrientation(camera, 90);


然后,在CameraConfigurationManager类的最后添加setDisplayOrientation()方法:
/**
 * 二维码90°旋转方法
 * @param camera
 * @param angle
 */
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);






第三步:


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;




第四步:
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中互换位置即可。


参考来自:http://blog.csdn.net/jdsjlzx/article/details/42645401



3.  二维码扫描图片变形:

Zxing  修改 CameraConfigurationManager.java文件的
void initFromCameraParameters(Camera camera)方法,
在 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);  
中的screenResolution改为  screenResolutionForCamera
如下:
cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);  

网上找了好久终于弄好了,希望有和我同样问题的可以更快的解决

你可能感兴趣的:(Android)