溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android如何從圖片獲取二維碼

發(fā)布時間:2020-07-18 15:39:30 來源:億速云 閱讀:188 作者:小豬 欄目:移動開發(fā)

這篇文章主要為大家展示了Android如何從圖片獲取二維碼,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

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

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

1.點(diǎn)擊按鈕后,跳轉(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.通過圖片路徑進(jìn)行解析圖片,獲取圖片的二維碼值。

//解析二維碼圖片,返回結(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設(shè)為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)在想取出來的圖片的邊長(二維碼圖片是正方形的)設(shè)置為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)存設(shè)置
   *
   * options.inPreferredConfig = Bitmap.Config.ARGB_4444;  // 默認(rèn)是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)換成二進(jì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來實(shí)現(xiàn),然后通過

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

將bitmap的二維碼轉(zhuǎn)換成圖片,然后又將圖片轉(zhuǎn)成二進(jìn)制圖片,調(diào)用QRCodeReader的result = reader.decode(binaryBitmap, hints);代碼把二進(jìn)制圖片轉(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)實(shí)應(yīng)用。
    //得到像素大小的字節(jié)數(shù)
    luminances = new byte[width * height];
    //得到圖片每點(diǎn)像素顏色
    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;
        //當(dāng)某一點(diǎn)三種顏色值相同時,相應(yīng)字節(jié)對應(yīng)空間賦值為其值
        if (r == g && g == b) {
          luminances[offset + x] = (byte) r;
        }
        //其它情況字節(jié)空間對應(yīng)賦值為:
        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;
  }
}

這樣就可以識別圖片的二維碼了,用這個功能一定要先導(dǎo)入ZXing jar包,這個很簡單,網(wǎng)上有很多介紹,大家自己可以查找一下。

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

就這么簡單。

以上就是關(guān)于Android如何從圖片獲取二維碼的內(nèi)容,如果你們有學(xué)習(xí)到知識或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI