ZXing 提供多種條碼的API供使用, 詳情與下載請參考 http://code.google.com/p/zxing/
1.圖檔解碼使用方式
2.文字編碼使用方式
1.圖檔解碼使用方式
private String readImage(String filename) { Bitmap bmp = BitmapFactory.decodeFile(filename);//自動轉換多種圖檔格式 jpg LuminanceSource source = new RGBLuminanceSource(bmp); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); // 解碼 try { Result result = (new QRCodeReader()).decode(bitmap); return result.getText(); } catch (ReaderException e) { return "[Error]解碼錯誤"; } }
2.文字編碼使用方式
至於使用 CCD 掃瞄解碼則請參考ZXing本身提供之範例public void encode(String contents, int width, int height) { BitMatrix matrix = null; // 取得資料矩陣 /*try {//指定編碼 Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); matrix = new MultiFormatWriter().encode(contents, com.google.zxing.BarcodeFormat.QR_CODE, width, height, hints); onDraw(matrix); } catch (com.google.zxing.WriterException e) { e.printStackTrace(); }*/ try {//使用預設編碼 matrix = new MultiFormatWriter().encode(contents, com.google.zxing.BarcodeFormat.QR_CODE, width, height); onDraw(matrix); } catch (Exception e) { e.printStackTrace(); } } //繪製 QRCode private void onDraw(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); int rate = 5; Paint p = new Paint(); newb = Bitmap.createBitmap( width*rate, height*rate, Config.ARGB_8888 ); Canvas canvasTemp = new Canvas( newb ); canvasTemp.drawColor(Color.TRANSPARENT); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { p.setColor(matrix.get(x, y) ? Color.BLACK : Color.WHITE); canvasTemp.drawRect(x*rate, y*rate, x*rate+rate, y*rate+rate, p); } } iv.setImageBitmap(newb); }