Android從圖片獲取二維碼的方法
之前的博客我記得講過關于掃描二維碼的內(nèi)容,昨天,組長讓我不僅可以掃描獲取二維碼,還可以通過圖片獲取里面的二維碼。比如別人拍了一張二維碼的照片,發(fā)送給你,app應該可以獲取圖片的二維碼。
自己在網(wǎng)上查了資料,發(fā)現(xiàn)其實也很簡單,用ZXing jar包里的獲取圖片二維碼的QRCodeReader就基本可以了。不過大部分的內(nèi)容,我自己也不明白,大家如果有興趣,可以自己去查找資料。
1.點擊按鈕后,跳轉(zhuǎn)到相冊,選擇有二維碼的圖片,返回到解析二維碼的界面。這時通過返回的URI獲取圖片的路徑。
case CHOOSE_PIC: String[] proj = new String[]{MediaStore.Images.Media.DATA}; Cursor cursor = QRCodeActivity.this.getContentResolver().query(data.getData(), proj, null, null, null); if(cursor.moveToFirst()){ int columnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); System.out.println(columnIndex); //獲取到用戶選擇的二維碼圖片的絕對路徑 imgPath = cursor.getString(columnIndex); } cursor.close(); //獲取解析結(jié)果 Result ret = parseQRcodeBitmap(imgPath); if (ret==null){ Toast.makeText(QRCodeActivity.this,getString(R.string.load_two_dimensional_error), Toast.LENGTH_LONG).show(); }else { // Toast.makeText(QRCodeActivity.this,"解析結(jié)果:" + ret.toString(), Toast.LENGTH_LONG).show(); Intent intent = new Intent(); intent.putExtra(Intents.Scan.RESULT, ret.toString()); this.setResult(Activity.RESULT_OK, intent); this.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); finish(); } break;
這個就是通過ContentResolver查詢URI獲取圖片的路徑,然后調(diào)用parseQRcodeBitmap(imgPath)獲取圖片的二維碼。
2.通過圖片路徑進行解析圖片,獲取圖片的二維碼值。
//解析二維碼圖片,返回結(jié)果封裝在Result對象中 private com.google.zxing.Result parseQRcodeBitmap(String bitmapPath){ //解析轉(zhuǎn)換類型UTF-8 Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); //獲取到待解析的圖片 BitmapFactory.Options options = new BitmapFactory.Options(); //如果我們把inJustDecodeBounds設為true,那么BitmapFactory.decodeFile(String path, Options opt) //并不會真的返回一個Bitmap給你,它僅僅會把它的寬,高取回來給你 options.inJustDecodeBounds = true; //此時的bitmap是null,這段代碼之后,options.outWidth 和 options.outHeight就是我們想要的寬和高了 Bitmap bitmap = BitmapFactory.decodeFile(bitmapPath,options); //我們現(xiàn)在想取出來的圖片的邊長(二維碼圖片是正方形的)設置為400像素 /** options.outHeight = 400; options.outWidth = 400; options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(bitmapPath, options); */ //以上這種做法,雖然把bitmap限定到了我們要的大小,但是并沒有節(jié)約內(nèi)存,如果要節(jié)約內(nèi)存,我們還需要使用inSimpleSize這個屬性 options.inSampleSize = options.outHeight / 400; if(options.inSampleSize <= 0){ options.inSampleSize = 1; //防止其值小于或等于0 } /** * 輔助節(jié)約內(nèi)存設置 * * options.inPreferredConfig = Bitmap.Config.ARGB_4444; // 默認是Bitmap.Config.ARGB_8888 * options.inPurgeable = true; * options.inInputShareable = true; */ options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(bitmapPath, options); //新建一個RGBLuminanceSource對象,將bitmap圖片傳給此對象 RGBLuminanceSource rgbLuminanceSource = new RGBLuminanceSource(bitmap); //將圖片轉(zhuǎn)換成二進制圖片 BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(rgbLuminanceSource)); //初始化解析對象 QRCodeReader reader = new QRCodeReader(); //開始解析 Result result = null; try { result = reader.decode(binaryBitmap, hints); } catch (Exception e) { // TODO: handle exception } return result; }
這里首先獲取圖片的bitmap,需要把獲取的bitmap專為一定的大小,通過options.inSampleSize來實現(xiàn),然后通過
//新建一個RGBLuminanceSource對象,將bitmap圖片傳給此對象 RGBLuminanceSource rgbLuminanceSource = new RGBLuminanceSource(bitmap); //將圖片轉(zhuǎn)換成二進制圖片 BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(rgbLuminanceSource)); //初始化解析對象 QRCodeReader reader = new QRCodeReader();
將bitmap的二維碼轉(zhuǎn)換成圖片,然后又將圖片轉(zhuǎn)成二進制圖片,調(diào)用QRCodeReader的result = reader.decode(binaryBitmap, hints);代碼把二進制圖片轉(zhuǎn)成二維碼,然后直接獲取返回值的字符串就是二維碼值。
其中用到了一個自定義的類RGBLuminanceSource,主要功能是將圖片的二維碼內(nèi)容獲取到,把除二維碼的內(nèi)容過濾,方便接下來的解析二維碼。
package com.zwcode.p6spro.util; import java.io.FileNotFoundException; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import com.google.zxing.LuminanceSource; public class RGBLuminanceSource extends LuminanceSource { private final byte[] luminances; public RGBLuminanceSource(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); //為了測量純解碼速度,我們將整個圖像灰度陣列前面,這是一樣的通道 // YUVLuminanceSource在現(xiàn)實應用。 //得到像素大小的字節(jié)數(shù) 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; //當某一點三種顏色值相同時,相應字節(jié)對應空間賦值為其值 if (r == g && g == b) { luminances[offset + x] = (byte) r; } //其它情況字節(jié)空間對應賦值為: else { luminances[offset + x] = (byte) ((r + g + g + b) >> 2); } } } } public RGBLuminanceSource(String path) throws FileNotFoundException { this(loadBitmap(path)); } @Override public byte[] getMatrix() { return luminances; } @Override public byte[] getRow(int arg0, byte[] arg1) { if (arg0 < 0 || arg0 >= getHeight()) { throw new IllegalArgumentException( "Requested row is outside the image: " + arg0); } int width = getWidth(); if (arg1 == null || arg1.length < width) { arg1 = new byte[width]; } System.arraycopy(luminances, arg0 * width, arg1, 0, width); return arg1; } 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; } }
這樣就可以識別圖片的二維碼了,用這個功能一定要先導入ZXing jar包,這個很簡單,網(wǎng)上有很多介紹,大家自己可以查找一下。
Android 從圖片獲取二維碼就講完了。
就這么簡單。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android利用ShaderMask實現(xiàn)花里胡哨的文字特效
我們的?App?大部分時候的文字都是一種顏色,實際上,文字的顏色也可以多姿多彩。我們今天就來介紹一個能夠輕松實現(xiàn)文字漸變色的組件?——?ShaderMask,感興趣的可以了解一下2022-12-12Android使用MulticastSocket實現(xiàn)多點廣播圖片
這篇文章主要為大家詳細介紹了Android使用MulticastSocket實現(xiàn)多點廣播圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01Android開發(fā)中ViewPager實現(xiàn)多頁面切換效果
ViewPager用于實現(xiàn)多頁面的切換效果,該類存在于Google的兼容包里面,所以在引用時記得在BuilldPath中加入“Android-support-v4.jar”。具體詳情大家可以參考下本文2016-11-11在Flutter中制作翻轉(zhuǎn)卡片動畫的完整實例代碼
最近Flutter的勢頭是越來越猛了,作為一個Android程序猿,我自然也是想要趕緊嘗試一把,這篇文章主要給大家介紹了關于在Flutter中制作翻轉(zhuǎn)卡片動畫的相關資料,需要的朋友可以參考下2021-10-10Android 實現(xiàn)背景圖和狀態(tài)欄融合方法
下面小編就為大家分享一篇Android 實現(xiàn)背景圖和狀態(tài)欄融合方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01解決Android中自定義DialogFragment解決寬度和高度問題
Android中自定義DialogFragment解決寬度和高度問題但是我們很多時候想把DialogFragment的高度固定,那么我們需要設置DialogFragment的高度,在Fragment的onResume()聲明周期方法中設置window的寬高即可2017-12-12Android利用listview控件操作SQLite數(shù)據(jù)庫實例
我們利用SQLiteOpenHelper類建立一個數(shù)據(jù)庫,并寫好增、刪、查等方法,通過SimpleCursorAdapter連接listview實現(xiàn)數(shù)據(jù)庫的增加、查詢以及長按刪除的功能。2017-04-04