Zxing仿微信二維碼掃描
1.Zxing的介紹
百忙之中抽出時間,我覺得還是得把zxing二維碼掃描總結一下。Zxing是Google推出來專門為二維碼這塊的開發很流行的一個框架,,它可以實現使用手機內建攝像頭完成二維碼的掃描和解碼。Zxing的專案地址:https://github.com/zxing/zxing2.二維碼的生成
1.生成二維碼工具類:/** * 生成二維碼工具類 * @param content * @param width * @param height * @return */ private Bitmap generateBitmap(String content,int width, int2.新增二維碼中間標誌的工具類:height) { QRCodeWriter qrCodeWriter = new QRCodeWriter(); Map<EncodeHintType, String> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); try { BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints); int[] pixels = new int[width * height]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (encode.get(j, i)) { pixels[i * width + j] = 0x00000000; } else { pixels[i * width + j] = 0xffffffff; } } } returnBitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565); } catch (WriterException e) { e.printStackTrace(); } return null; }
/** * 新增二維碼中心logo工具類 * @param qrBitmap * @param logoBitmap * @return */ private Bitmap addLogo(Bitmap qrBitmap, Bitmap logoBitmap) { int qrBitmapWidth = qrBitmap.getWidth(); int qrBitmapHeight = qrBitmap.getHeight(); int logoBitmapWidth = logoBitmap.getWidth(); int logoBitmapHeight = logoBitmap.getHeight(); Bitmap blankBitmap = Bitmap.createBitmap(qrBitmapWidth, qrBitmapHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(blankBitmap); canvas.drawBitmap(qrBitmap, 0, 0, null); canvas.save(Canvas.ALL_SAVE_FLAG); float scaleSize = 1.0f; while ((logoBitmapWidth / scaleSize) > (qrBitmapWidth / 5) || (logoBitmapHeight / scaleSize) > (qrBitmapHeight / 5)) { scaleSize *= 2; } float sx = 1.0f / scaleSize; canvas.scale(sx, sx, qrBitmapWidth / 2, qrBitmapHeight / 2); canvas.drawBitmap(logoBitmap, (qrBitmapWidth - logoBitmapWidth) / 2, (qrBitmapHeight - logoBitmapHeight) / 2, null); canvas.restore(); return blankBitmap; }ok,兩個方法就搞定了二維碼的生成:
3.二維碼的識別
識別的話就稍微難點,首先把zxing開源專案的識別程式碼整合到我們的模組中,具體步驟:
1.從github中把Zxing 的開源專案下載下來
2.新建一個新工程,然後在主介面自定義一個跳轉Button,用來實現掃描的跳轉
3.在剛才新建的專案中建立一個module庫。
選擇第二項,新建一個zxing依賴庫 4.將zxing開源專案裡的android,android-core裡面的檔案複製到依賴庫的相對應的資料夾
其中android中要複製的檔案:
android-core中要複製的檔案
全部複製到依賴庫中後:
6.將工程與依賴庫的最小適配版本統一,然後將工程build.gradle的依賴包全都刪除,直接依賴Zxing庫: ①工程中:
②依賴庫中:
7.再把依賴庫中manifest清單檔案中application的icon屬性移除,然後編譯一把 8.這時還是會報錯,最後你把依賴庫的程式碼中switch 全部替換成 if.....else if....else的形式,這時就整合好了。 然後呢,效果出來了,你卻能發現掃碼是橫屏的,shit,這也是夠了,哈哈,不過彆著急,慢慢來,接下來就把橫屏轉為我們習慣的豎屏: 1.在DecodeHandler.java中,修改decode方法
PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, height);之前新增
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;
2.在CameraManager.java中,註釋程式碼:
//
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.java中,在setDesiredCameraParameters方法中新增一句
camera.setDisplayOrientation(90);
4.在AndroidManifest.xml中,把Activity的屬性android:screenOrientation="landscape"
改為
android:screenOrientation="portrait"