長按識別圖中二維碼
阿新 • • 發佈:2019-02-14
iv_zxingimag .setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View viewm) { saveCurrentImage(); return false; } });
private void saveCurrentImage() { //獲取當前螢幕的大小 int width = getWindow().getDecorView().getRootView().getWidth(); intheight = getWindow().getDecorView().getRootView().getHeight(); //生成相同大小的圖片 Bitmap temBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); //找到當前頁面的根佈局 View view = getWindow().getDecorView().getRootView(); //設定快取 view.setDrawingCacheEnabled(true); view.buildDrawingCache(); //從快取中獲取當前螢幕的圖片,建立一個DrawingCache的拷貝,因為DrawingCache得到的點陣圖在禁用後會被回收temBitmap = view.getDrawingCache(); SimpleDateFormat df = new SimpleDateFormat("yyyymmddhhmmss"); final String time = df.format(new Date()); if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/screen", time + ".png"); if (!file.exists()) { file.getParentFile().mkdirs(); try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } FileOutputStream fos = null; try { fos = new FileOutputStream(file); temBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } new Thread(new Runnable() { @Override public void run() { String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/screen/" + time + ".png"; final Result result = parseQRcodeBitmap(path); runOnUiThread(new Runnable() { public void run() { if (null != result) { Toast.makeText(TakewayActivity.this, result.toString(), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(TakewayActivity.this, "無法識別", Toast.LENGTH_LONG).show(); } } }); } }).start(); //禁用DrawingCahce否則會影響效能 ,而且不禁止會導致每次截圖到儲存的是第一次截圖快取的點陣圖 view.setDrawingCacheEnabled(false); } } //解析二維碼圖片,返回結果封裝在Result物件中 private 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(); options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(bitmapPath, options); options.inSampleSize = options.outHeight / 400; if (options.inSampleSize <= 0) { options.inSampleSize = 1; //防止其值小於或等於0 } 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; }
public class RGBLuminanceSource extends LuminanceSource { private byte bitmapPixels[]; protected RGBLuminanceSource(Bitmap bitmap) { super(bitmap.getWidth(), bitmap.getHeight()); // 首先,要取得該圖片的畫素陣列內容 int[] data = new int[bitmap.getWidth() * bitmap.getHeight()]; this.bitmapPixels = new byte[bitmap.getWidth() * bitmap.getHeight()]; bitmap.getPixels(data, 0, getWidth(), 0, 0, getWidth(), getHeight()); // 將int陣列轉換為byte陣列,也就是取畫素值中藍色值部分作為辨析內容 for (int i = 0; i < data.length; i++) { this.bitmapPixels[i] = (byte) data[i]; } } @Override public byte[] getMatrix() { // 返回我們生成好的畫素資料 return bitmapPixels; } @Override public byte[] getRow(int y, byte[] row) { // 這裡要得到指定行的畫素資料 System.arraycopy(bitmapPixels, y * getWidth(), row, 0, getWidth()); return row; } }