麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 系統(tǒng) > Android > 正文

Android平臺(tái)生成二維碼并實(shí)現(xiàn)掃描 & 識(shí)別功能

2019-10-23 20:36:57
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1.二維碼的前世今生

“二維條碼/二維碼(2-dimensional bar code)是用某種特定的幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號(hào)信息的;在代碼編制上巧妙地利用構(gòu)成計(jì)算機(jī)內(nèi)部邏輯基礎(chǔ)的“0”、“1”比特流的概念,使用若干個(gè)與二進(jìn)制相對(duì)應(yīng)的幾何形體來(lái)表示文字?jǐn)?shù)值信息,通過(guò)圖象輸入設(shè)備或光電掃描設(shè)備自動(dòng)識(shí)讀以實(shí)現(xiàn)信息自動(dòng)處理:它具有條碼技術(shù)的一些共性:每種碼制有其特定的字符集;每個(gè)字符占有一定的寬度;具有一定的校驗(yàn)功能等。同時(shí)還具有對(duì)不同行的信息自動(dòng)識(shí)別功能、及處理圖形旋轉(zhuǎn)變化點(diǎn)。 [1] ”

上面是百度百科的解釋。既然有二維碼,那么肯定有一維碼。

一維碼。最為常見(jiàn)的就是食品 & 書本后面的條碼。

條碼起源與20世紀(jì)40年代,后來(lái)在1970年 UPC碼發(fā)明,并開(kāi)始廣泛應(yīng)用與食品包裝。

具體的介紹可以看百度百科 一維碼。

其實(shí)二維碼與一維碼本質(zhì)上是類似的,就跟一維數(shù)組和二維數(shù)組一樣。

2.二維碼的java支持庫(kù)

為了讓java或者說(shuō)android方便繼承條碼的功能,google就開(kāi)發(fā)了一個(gè)zxing的庫(kù):

https://github.com/zxing/zxing

3.生成二維碼

public class EncodeThread {public static void encode(final String url, final int width, final int height, final EncodeResult result) {if (result == null) {return;}if (TextUtils.isEmpty(url)) {result.onEncodeResult(null);return;}new Thread() {@Overridepublic void run() {try {MultiFormatWriter writer = new MultiFormatWriter();Hashtable<EncodeHintType, String> hints = new Hashtable<>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");BitMatrix bitMatrix = writer.encode(url, BarcodeFormat.QR_CODE, width, height, hints);Bitmap bitmap = parseBitMatrix(bitMatrix);result.onEncodeResult(bitmap);return;} catch (WriterException e) {e.printStackTrace();}result.onEncodeResult(null);}}.start();}/*** 生成二維碼內(nèi)容<br>** @param matrix* @return*/public static Bitmap parseBitMatrix(BitMatrix matrix) {final int QR_WIDTH = matrix.getWidth();final int QR_HEIGHT = matrix.getHeight();int[] pixels = new int[QR_WIDTH * QR_HEIGHT];//this we using qrcode algorithmfor (int y = 0; y < QR_HEIGHT; y++) {for (int x = 0; x < QR_WIDTH; x++) {if (matrix.get(x, y)) {pixels[y * QR_WIDTH + x] = 0xff000000;} else {pixels[y * QR_WIDTH + x] = 0xffffffff;}}}Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);return bitmap;}public interface EncodeResult {void onEncodeResult(Bitmap bitmap);}} 

zxing 支持很多條碼格式:我們這里使用QR_CODE碼。也就是我們常見(jiàn)的微信里面的二維碼。

我們先來(lái)分析下這段代碼:

MultiFormatWriter writer = new MultiFormatWriter();

這個(gè)是一個(gè)工具類,把所有支持的幾個(gè)write寫在里面了。

public BitMatrix encode(String contents,BarcodeFormat format,int width, int height,Map<EncodeHintType,?> hints) throws WriterException {Writer writer;switch (format) {case EAN_8:writer = new EAN8Writer();break;case UPC_E:writer = new UPCEWriter();break;case EAN_13:writer = new EAN13Writer();break;case UPC_A:writer = new UPCAWriter();break;case QR_CODE:writer = new QRCodeWriter();break;case CODE_39:writer = new Code39Writer();break;case CODE_93:writer = new Code93Writer();break;case CODE_128:writer = new Code128Writer();break;case ITF:writer = new ITFWriter();break;case PDF_417:writer = new PDF417Writer();break;case CODABAR:writer = new CodaBarWriter();break;case DATA_MATRIX:writer = new DataMatrixWriter();break;case AZTEC:writer = new AztecWriter();break;default:throw new IllegalArgumentException("No encoder available for format " + format);}return writer.encode(contents, format, width, height, hints);} 

這是官方最新支持的格式,具體看引入的jar里面支持的格式。

對(duì)與bitmatrix的結(jié)果,通過(guò)摸個(gè)算法,設(shè)置每個(gè)點(diǎn)白色,或者黑色。

最后創(chuàng)建一張二維碼的圖片。

4.識(shí)別二維碼

如何從一張圖片上面,識(shí)別二維碼呢:

public class ReDecodeThread {public static void encode(final Bitmap bitmap, final ReDecodeThreadResult listener) {if (listener == null) {return;}if (bitmap == null) {listener.onReDecodeResult(null);return;}new Thread() {@Overridepublic void run() {try {MultiFormatReader multiFormatReader = new MultiFormatReader();BitmapLuminanceSource source = new BitmapLuminanceSource(bitmap);BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));Result result1 = multiFormatReader.decode(bitmap1);listener.onReDecodeResult(result1.getText());return;} catch (NotFoundException e) {e.printStackTrace();}listener.onReDecodeResult(null);}}.start();}public interface ReDecodeThreadResult {void onReDecodeResult(String url);}} 

過(guò)程也是很簡(jiǎn)單,使用MultiFormatReader來(lái)分析圖片,這里不需要缺人圖片的條碼格式。

如果分析下源碼,就是依次使用每種格式的reader來(lái)分析,直到找到合適的為止。

當(dāng)然回了能夠把Bitmap轉(zhuǎn)化成Bitmatrix,然后在分析。

public final class BitmapLuminanceSource extends LuminanceSource{private final byte[] luminances;public BitmapLuminanceSource(String path) throws FileNotFoundException {this(loadBitmap(path));}public BitmapLuminanceSource(Bitmap bitmap) {super(bitmap.getWidth(), bitmap.getHeight());int width = bitmap.getWidth();int height = bitmap.getHeight();int[] pixels = new int[width * height];bitmap.getPixels(pixels, 0, width, 0, 0, width, height);// In order to measure pure decoding speed, we convert the entire image// to a greyscale array// up front, which is the same as the Y channel of the// YUVLuminanceSource in the real app.luminances = new byte[width * height];for (int y = 0; y < height; y++) {int offset = y * width;for (int x = 0; x < width; x++) {int pixel = pixels[offset + x];int r = (pixel >> 16) & 0xff;int g = (pixel >> 8) & 0xff;int b = pixel & 0xff;if (r == g && g == b) {// Image is already greyscale, so pick any channel.luminances[offset + x] = (byte) r;} else {// Calculate luminance cheaply, favoring green.luminances[offset + x] = (byte) ((r + g + g + b) >> 2);}}}}@Overridepublic byte[] getRow(int y, byte[] row) {if (y < 0 || y >= getHeight()) {throw new IllegalArgumentException("Requested row is outside the image: " + y);}int width = getWidth();if (row == null || row.length < width) {row = new byte[width];}System.arraycopy(luminances, y * width, row, 0, width);return row;}// Since this class does not support cropping, the underlying byte array// already contains// exactly what the caller is asking for, so give it to them without a copy.@Overridepublic byte[] getMatrix() {return luminances;}private static Bitmap loadBitmap(String path) throws FileNotFoundException {Bitmap bitmap = BitmapFactory.decodeFile(path);if (bitmap == null) {throw new FileNotFoundException("Couldn't open " + path);}return bitmap;}} 

5.掃描二維碼

掃描二維碼,其實(shí)比上面只多了一步,就是把camera獲取的東西直接轉(zhuǎn)換,然后進(jìn)行識(shí)別。

public void requestPreviewFrame(Handler handler, int message) {if (camera != null && previewing) {previewCallback.setHandler(handler, message);if (useOneShotPreviewCallback) {camera.setOneShotPreviewCallback(previewCallback);} else {camera.setPreviewCallback(previewCallback);}}} 

首先把camera預(yù)覽的數(shù)據(jù)放入previewCallback中。

final class PreviewCallback implements Camera.PreviewCallback public void onPreviewFrame(byte[] data, Camera camera) {Point cameraResolution = configManager.getCameraResolution();if (!useOneShotPreviewCallback) {camera.setPreviewCallback(null);}if (previewHandler != null) {Message message = previewHandler.obtainMessage(previewMessage, cameraResolution.x,cameraResolution.y, data);message.sendToTarget();previewHandler = null;} else {Log.d(TAG, "Got preview callback, but no handler for it");}} 

可以看到,預(yù)覽的數(shù)據(jù)data,回傳遞過(guò)來(lái),然后handler的方式傳遞出去。

接收data的地方:

@Overridepublic void handleMessage(Message message) {switch (message.what) {case R.id.decode://Log.d(TAG, "Got decode message");decode((byte[]) message.obj, message.arg1, message.arg2);break;case R.id.quit:Looper.myLooper().quit();break;}} 

然后是decode data

private void decode(byte[] data, int width, int height) {long start = System.currentTimeMillis();Result rawResult = null;//modify herebyte[] 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 #11width = height;height = tmp;PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));try {rawResult = multiFormatReader.decodeWithState(bitmap);} catch (ReaderException re) {// continue} finally {multiFormatReader.reset();}if (rawResult != null) {long end = System.currentTimeMillis();Log.d(TAG, "Found barcode (" + (end - start) + " ms):/n" + rawResult.toString());Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, rawResult);Bundle bundle = new Bundle();bundle.putParcelable(DecodeThread.BARCODE_BITMAP, source.renderCroppedGreyscaleBitmap());message.setData(bundle);//Log.d(TAG, "Sending decode succeeded message...");message.sendToTarget();} else {Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);message.sendToTarget();}} 

當(dāng)把camera上的圖片轉(zhuǎn)換成BinaryBitmap以后,剩下的事情,就更直接從圖片識(shí)別是一樣的。

PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 日韩.www | 一级毛片在线观看免费 | 欧美雌雄另类xxxxx | 在线成人一区 | 9191久久久久视频 | 一级电影在线观看 | 欧洲成人一区二区 | 国产精品无码久久久久 | 久久精品国产99国产精品亚洲 | 久久精品视频在线 | 激情网站在线观看 | 天天躁狠狠躁夜躁2020挡不住 | 91情侣偷在线精品国产 | 日韩黄色成人 | 成人精品免费在线观看 | 一级黄色淫片 | 欧美精品免费一区二区三区 | 色播视频在线播放 | 国产欧美精品一区二区三区四区 | 精品一区二区在线播放 | 国产精品久久久久久久久久久天堂 | 国产一区二区高清在线 | 精品成人国产在线观看男人呻吟 | 亚洲第一精品在线 | 国产成人自拍av | 成人羞羞在线观看网站 | 亚洲成人免费网站 | 老女人碰碰在线碰碰视频 | 日本xxxx色视频在线观看免费, | 九九热视频在线免费观看 | www视频免费在线观看 | 青热久思思 | 黄色成人小视频 | 亚洲成人夜色 | 国产一精品一av一免费爽爽 | 精品一区二区三区免费毛片爱 | 中国成人在线视频 | 毛片大全在线观看 | 精品无码久久久久久国产 | 欧洲色阁中文字幕 | 久久久综合久久久 |