使用zxing扫描二维码,需要远距离扫描才能识别,用户体验不好,在网上搜了一下,发现一个解决办法,最起码现在是可以近距离扫描了。
1.修改扫描框大小,使用各种分辨率
CameraManager.java类中修改
public Rect getFramingRect() {
if (framingRect == null) {
if (camera == null) {
return null;
}
Point screenResolution = configManager.getScreenResolution();
if (screenResolution == 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) / 3;
framingRect = new Rect(leftOffset, topOffset, leftOffset + width,
topOffset + height);
Log.d(TAG, "Calculated framing rect: " + framingRect);
}
return framingRect;
}
修改扫描距离
CameraConfigurationManager.java
void setDesiredCameraParameters(Camera camera) {
Camera.Parameters parameters = camera.getParameters();
List
int position =0;
if(supportedPreviewSizes.size()>2){
position=supportedPreviewSizes.size()/2+1;//supportedPreviewSizes.get();
}else {
position=supportedPreviewSizes.size()/2;
}
int width = supportedPreviewSizes.get(position).width;
int height = supportedPreviewSizes.get(position).height;
Log.d(TAG, "Setting preview size: " + cameraResolution);
camera.setDisplayOrientation(90);
cameraResolution.x=width;
cameraResolution.y=height;
parameters.setPreviewSize(width,height);
setFlash(parameters);
setZoom(parameters);
//setSharpness(parameters);
camera.setParameters(parameters);
}