1、下載下傳源碼
https://github.com/zxing/zxing
2、解壓後建立java工程,導入zxing-core下的源碼,導出jar檔案。
3、導入Android工程,引入2中的jar檔案,将android-core下的CameraConfigurationUtils.java拷貝到對應的位置,編譯OK。
4、修正manifest為豎屏顯示。
<activity android:name=".CaptureActivity"
android:screenOrientation="portrait"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:theme="@style/CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden">
5、修正 CameraConfigurationUtils.java的findBestPreviewSizeValue()方法。
因為realWidth和realHeight是從camera的reviewSize中取值,是以總是橫屏。根據螢幕尺寸進行修正。
<pre name="code" class="html" style="font-size: 14px;">// boolean isCandidatePortrait = realWidth < realHeight;
boolean isCandidatePortrait = screenResolution.x < screenResolution.y;
6、修正 CameraManager.java的getFramingRectInPreview()方法。
cameraResolution儲存了攝像頭傳回的圖檔尺寸,需要進行旋轉對應。
if (screenResolution.x < screenResolution.y) {
<span style="white-space:pre"> </span>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;
} else {
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;
}
7、修正DecodeHandler.java的decode()方法。
圖檔向右旋轉90度,并注意修正旋轉後的圖檔寬度和高度。
// <span style="font-family: Monaco;">PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(data, width, height);</span>
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];
}
PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotatedData, height, width);
8、豎屏掃描的問題解決後,可以根據自己的需要定制開發二維碼掃描的庫了。