Android Zxing修改为竖屏扫描,和连续扫描问题

1.在清单配置文件当中

android:screenOrientation="landscape"修改为android:screenOrientation="portrait"


2.修改CameraManager类,在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;

3.修改CameraConfigurationManager类,在initFromCameraParameters方法中

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,
				screenResolutionForCamera);
然后再setDesiredCameraParameters方法当中加入一句代码

setDisplayOrientation(camera, 90);//把摄像头视角旋转90度

然后再这个类里面加入上面的方法

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) {
			e1.printStackTrace();
		}
	}

4.修改DecodeHandler类文件,在decode(byte[] data, int width, int height)方法中

在buildLuminanceSource方法调用前面加入下面的代码

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;
		data = rotatedData;
		PlanarYUVLuminanceSource source = CameraManager.get()
				.buildLuminanceSource(rotatedData, width, height);
然后注释掉原来的 buildLuminanceSource这行调用代码。

到这里竖屏代码就修改完成了,如果想修改扫描框的大小,可以去CameraManager这个类当中,修改这些常量就可以了

	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;

====================================================================

Zxing扫描一次二维码或条形码完成之后,想要继续扫描的话,就需要重新进入这个扫描页面,很麻烦,想要实现连续扫描的话,在CaptureActivity类当中加入下面这个方法:

/**
	 * 使Zxing能够继续扫描
	 */
	public void continuePreview() {
		if (handler != null) {
			handler.restartPreviewAndDecode();
		}
	}
这里的handler就是CaptureActivityHandler类的实例,在合适的地方调用这个方法就可以连续的进行二维码的扫描了。


你可能感兴趣的:(Android笔记)