Android Zxing自定義二維碼識別(二)

一、自定義字符串生成二維碼

 /**
     * 使用 ZXing Android Embedded 生成二維碼
     *
     * @param s
     * @return
     */
    public Bitmap encodeBitmap(String s) {
        Bitmap bitmap = null;
        BitMatrix result = null;
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        try {
            result = multiFormatWriter.encode(s, BarcodeFormat.QR_CODE, DensityUtils.dp2px(this, 300), DensityUtils.dp2px(this, 300));
            BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
            bitmap = barcodeEncoder.createBitmap(result);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException iae) {
            return null;
        }
        return bitmap;
    }

二、選擇本地圖片識別

/**
     * 打開相冊
     */

    public void openGallery() {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.setType("image/*");
        photoPickerIntent.setAction(Intent.ACTION_GET_CONTENT);
        try {
            LogUtil.d(TAG, "can take picture");
            startActivityForResult(photoPickerIntent, REQUEST_CODE_GALLERY);
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.showMessage("無法打開相冊");
        }
    }
  • 識別圖片內的字符串
/**
     * 圖片解碼
     *
     * @param data
     * @return
     */
    public String decodeImage(Intent data) {
        Result result = null;
        String codeString = "";

        try {
            if (data != null) {
                File mFileTemp = new File(FileAccessor.IMAGE + "code" + FileAccessor.JPG);
                InputStream inputStream = getContentResolver().openInputStream(data.getData());
                FileOutputStream fileOutputStream = new FileOutputStream(mFileTemp);
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, bytesRead);
                }
                fileOutputStream.close();
                inputStream.close();
                Bitmap bitmap = ImageFileCacheUtils.getInstance().getBitmap(mFileTemp);
                try {
                    if (bitmap != null) {
                        int width = bitmap.getWidth();
                        int height = bitmap.getHeight();
                        int[] pixels = new int[width * height];
                        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
                        RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
                        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
                        Reader reader = new QRCodeReader();
                        try {
                            result = reader.decode(binaryBitmap);
                        } catch (NotFoundException | ChecksumException | FormatException e) {
                            e.printStackTrace();
                        }
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            LogUtil.e(TAG, "Error while creating temp file");
        }
        if (result != null) {
            codeString = result.getText();
        }
        return codeString;
    }
/**
     * 獲取bitmap
     *
     * @param file 文件
     * @return bitmap
     */
    public Bitmap getBitmap(File file) {
        if (file == null) {
            return null;
        }
        InputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(file));
            return BitmapFactory.decodeStream(is);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } finally {
            CloseUtils.closeIO(is);
        }
    }
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網絡請求組件 FMDB本地數據庫組件 SD...
    陽明AI閱讀 16,210評論 3 119
  • 發(fā)現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,399評論 4 61
  • 又到歲末盤點、總結和計劃的時間了,每天為了事業(yè)和家庭忙忙碌碌,總想稍稍停下腳步細細品味時光,幸好在行色匆匆中也能把...
    陳思梅閱讀 624評論 1 0
  • 一頭白發(fā),走路顫顫悠悠的阿寶,已經九十歲了。 活到這個歲數的人,都是有福的。但是,于目不識丁一生都沒出...
    大眼鈴閱讀 335評論 0 0
  • 最近在重溫《歡樂頌》,一直很喜歡安迪"大女人"的氣場,但第二次看,卻被劇中的乖乖女關雎爾吸引。她從小家境優(yōu)渥,在5...
    墨子爵閱讀 272評論 4 1

友情鏈接更多精彩內容