国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android從圖片獲取二維碼的方法

2019-10-23 18:29:12
字體:
來源:轉載
供稿:網友

之前的博客我記得講過關于掃描二維碼的內容,昨天,組長讓我不僅可以掃描獲取二維碼,還可以通過圖片獲取里面的二維碼。比如別人拍了一張二維碼的照片,發送給你,app應該可以獲取圖片的二維碼。

自己在網上查了資料,發現其實也很簡單,用ZXing jar包里的獲取圖片二維碼的QRCodeReader就基本可以了。不過大部分的內容,我自己也不明白,大家如果有興趣,可以自己去查找資料。

1.點擊按鈕后,跳轉到相冊,選擇有二維碼的圖片,返回到解析二維碼的界面。這時通過返回的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();          //獲取解析結果          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,"解析結果:" + 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獲取圖片的路徑,然后調用parseQRcodeBitmap(imgPath)獲取圖片的二維碼。

2.通過圖片路徑進行解析圖片,獲取圖片的二維碼值。

//解析二維碼圖片,返回結果封裝在Result對象中private com.google.zxing.Result parseQRcodeBitmap(String bitmapPath){  //解析轉換類型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);  //我們現在想取出來的圖片的邊長(二維碼圖片是正方形的)設置為400像素  /**   options.outHeight = 400;   options.outWidth = 400;   options.inJustDecodeBounds = false;   bitmap = BitmapFactory.decodeFile(bitmapPath, options);   */  //以上這種做法,雖然把bitmap限定到了我們要的大小,但是并沒有節約內存,如果要節約內存,我們還需要使用inSimpleSize這個屬性  options.inSampleSize = options.outHeight / 400;  if(options.inSampleSize <= 0){    options.inSampleSize = 1; //防止其值小于或等于0  }  /**   * 輔助節約內存設置   *   * 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);  //將圖片轉換成二進制圖片  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來實現,然后通過

//新建一個RGBLuminanceSource對象,將bitmap圖片傳給此對象RGBLuminanceSource rgbLuminanceSource = new RGBLuminanceSource(bitmap);//將圖片轉換成二進制圖片BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(rgbLuminanceSource));//初始化解析對象QRCodeReader reader = new QRCodeReader();

將bitmap的二維碼轉換成圖片,然后又將圖片轉成二進制圖片,調用QRCodeReader的result = reader.decode(binaryBitmap, hints);代碼把二進制圖片轉成二維碼,然后直接獲取返回值的字符串就是二維碼值。

其中用到了一個自定義的類RGBLuminanceSource,主要功能是將圖片的二維碼內容獲取到,把除二維碼的內容過濾,方便接下來的解析二維碼。

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在現實應用。    //得到像素大小的字節數    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) {          luminances[offset + x] = (byte) r;        }        //其它情況字節空間對應賦值為:        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包,這個很簡單,網上有很多介紹,大家自己可以查找一下。

Android 從圖片獲取二維碼就講完了。

就這么簡單。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青田县| 沾益县| 海丰县| 旬阳县| 郧西县| 潼关县| 横山县| 吴川市| 玉溪市| 洱源县| 乌拉特中旗| 简阳市| 曲周县| 象山县| 凤翔县| 确山县| 象州县| 邓州市| 广丰县| 永年县| 合山市| 娄底市| 永泰县| 丰县| 阿合奇县| 施甸县| 富川| 霍城县| 房产| 南木林县| 玛纳斯县| 通辽市| 当雄县| 司法| 萨嘎县| 兴海县| 金乡县| 砀山县| 伊春市| 会东县| 营山县|