1.? ?功能概述
默認的ZXing Demo提供的是橫屏掃描没佑,不符合現在市場的流行趨勢和用戶的使用習慣滨攻,然而在修改界面為豎屏的過程中,我們發(fā)現zxing無法讀取條形碼數據(二維碼可以正常讀刃铩)真竖。修改布局的過程不再贅述。
2.? 修改說明
1) 在AndroidManifest.xml中详幽,把Activity的屬性的screenOrientation設置為"portrait"筛欢。在開發(fā)android的應用中,有時候需要限制橫豎屏切換唇聘。只需要在AndroidManifest.xml文件中加入android:screenOrientation屬性限制版姑。其中android:screenOrientation="portrait"是限制此頁面豎屏顯示。
2) 在CameraManager.java文件中迟郎,修改預覽縮放剥险。主要根據長寬比對預覽效果進行了分類。如果不需要橫屏了宪肖,可以直接取豎屏的設置表制。主要代碼如下:
rect.left = framingRect.left * cameraResolution.x / screenResolution.x;
rect.right = framingRect.right * cameraResolution.x / screenResolution.x;
rect.top = framingRect.top * cameraResolution.y / screenResolution.y;
rect.bottom = framingRect.bottom * cameraResolution.y / screenResolution.y;
改為:
?// 下面為豎屏模式
if (screenResolution.x < screenResolution.y) {
????rect.left = framingRect.left * cameraResolution.y / screenResolution.x;
????rect.right = framingRect.right * cameraResolution.y / screenResolution.x;
????rect.top = framingRect.top * cameraResolution.x / screenResolution.y;
????rect.bottom = framingRect.bottom * cameraResolution.x / screenResolution.y;
} else {
????// 下面為橫屏模式?
????rect.left = framingRect.left * cameraResolution.x / screenResolution.x;
????rect.right = framingRect.right * cameraResolution.x / screenResolution.x;
????rect.top = framingRect.top * cameraResolution.y / screenResolution.y;
????rect.bottom = framingRect.bottom * cameraResolution.y / screenResolution.y;
}
3) 在CameraManager.java文件中,修改解析函數?buildLuminanceSource(),這是本次修改的核心控乾,因為默認的條形碼解析是橫屏的么介,在本次修改中,我們根據豎屏條件做了一些針對性的處理,主要功能是將獲得數據的寬和高置換蜕衡,使其適應豎屏環(huán)境壤短。核心代碼如下:
public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
? ? ? ? Rect rect = getFramingRectInPreview();
? ? ? ? if (rect == null) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ?PlanarYUVLuminanceSource source;
? ? ? ? Point point = configManager.getScreenResolution();
? ? ? ? if (point.x < point.y) {
? ? ? ? ????byte[] rotatedData = new byte[data.length];
? ? ? ? ? ? int newWidth = height;
? ? ? ? ? ? int newHeight = width;
? ? ? ? ? ? for (int y = 0; y < height; y++) {
? ? ? ? ? ? ? ? for (int x = 0; x < width; x++)
? ? ? ? ? ? ? ? ? ? rotatedData[x * newWidth + newWidth - 1 - y] = data[x + y * width];
? ? ? ? ? ? }
? ? ? ? ? ? source = new PlanarYUVLuminanceSource(rotatedData, newWidth, newHeight,
? ? ? ? ? ? ? ? ? ? rect.left, rect.top, rect.width(), rect.height(), false);
? ? ? ? } else {
? ? ? ? ? ? source = new PlanarYUVLuminanceSource(data, width, height,
? ? ? ? ? ? ? ? ? ? rect.left, rect.top, rect.width(), rect.height(), false);
? ? ? ? }
? ? ? ?return source;
? ? }
4) 在CameraConfigurationUtils.java文件中,需要在findBestPreviewSizeValue方法中將screenAspectRatio分情況設置,因為screenAspectRatio的要求總是大值比小值久脯。代碼如下:
將
double screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;
修改為:
double screenAspectRatio;
if (screenResolution.x < screenResolution.y) { // 豎屏? ? ? ? ? ?
? ? screenAspectRatio = (double) screenResolution.y / (double) screenResolution.x;
} else {
? ? screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;
}
5) 在CameraConfigurationUtils.java文件中蒜绽,刪除如下代碼,原因是對于鏡頭分辨率高桶现,而屏幕分辨率低的手機躲雅,這段代碼直接導致掃碼插件采用較低的分辨率去生成用于解析的位圖,所以直接去掉骡和。然后你就會發(fā)現低分辨率的手機相赁,對二維碼、條碼的識別率顯著提高慰于。
if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {
? ? ? ? Point exactPoint = new Point(realWidth, realHeight);
? ? ? ? Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);
? ? ? ? return exactPoint;
}