jar包没有自己编译,看了下挺麻烦就从网上找了个2.2的jar包。已经放到工程里了,需要的可以去下载工程从libs里直接找就好了。
1、 Zxing的扫描界面就是CaptureActivity.java,这个类在主包下即com.google.zxing.client.android。去AndroidManifest.xml里将其强制横屏的代码改为强制竖屏。即 android:screenOrientation="landscape" 改为 android:screenOrientation="portrait"
2、接下来修改camera相关的。找到com.google.zxing.client.android.camera下CameraConfigurationManager.java
这个文件,Camera默认起来是横屏的,这时候我们要在setDesiredCameraParameters这个方法里在camera.setParameters(parameters);这个语句之前加上 camera.setDisplayOrientation(90);做过Camera开发的朋友都知道。在initFromCameraParameters方法里将宽高替换下。最后四句的代码如下
screenResolution = new Point(height, width); Log.i(TAG, "Screen resolution: " + screenResolution); cameraResolution = findBestPreviewSizeValue(parameters, new Point(width, height)); Log.i(TAG, "Camera resolution: " + cameraResolution);
找到com.google.zxing.client.android.camera下CameraManager.java,修改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、在com.google.zxing.client.android下的DecodeHandler.java下修改decode方法。修改方法如下:
在Result rawResult = null之前PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);之前加入以下语句:
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; width = height; height = tmp; data = rotatedData;</pre><br>
然后编译运行,修改竖屏就大功告成了。不过这个工程里还有很多臃肿的代码,后续摘除再做分享。
代码地址:
http://download.csdn.net/detail/liuhanhan512/6691223