上一篇講到:Android二維碼開源項目zxing編譯,編譯出來後有一個自帶的測試程序:CaptureActivity比較複雜,我僅僅要是把一些不用的東西去掉,用看起來更方便,二維碼和條形碼的流行性自沒必要說了。java
自帶的樣例,文件夾結構例如如下:android
改動後的程序文件夾結構,去掉了很是多功能,假設分享、設置等。算法
先上效果圖chrome
掃描ZXing生成的條形碼和二維碼結果瀏覽器
掃描界面app
掃描商品的條碼ide
整個程序僅僅改動了下面兩個類,其餘都是直接從原來的Demo中複製出來的ui
生成二維碼的代碼this
/** * 生成二維碼 要轉換的地址或字符串,可以是中文 * * @param url * @param width * @param height * @return */ public Bitmap createQRImage(String url, final int width, final int height) { try { // 推斷URL合法性 if (url == null || "".equals(url) || url.length() < 1) { return null; } Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 圖像數據轉換,使用了矩陣轉換 BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints); int[] pixels = new int[width * height]; // 如下這裏依照二維碼的算法,逐個生成二維碼的圖片, // 兩個for循環是圖片橫列掃描的結果 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (bitMatrix.get(x, y)) { pixels[y * width + x] = 0xff000000; } else { pixels[y * width + x] = 0xffffffff; } } } // 生成二維碼圖片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; }可以生成中文
生成條形碼關鍵代碼,下面代碼是把條形碼和條形碼下的文字合併成一張圖片顯示。編碼
/** * 生成條形碼 * * @param context * @param contents * 需要生成的內容 * @param desiredWidth * 生成條形碼的寬帶 * @param desiredHeight * 生成條形碼的高度 * @param displayCode * 是否在條形碼下方顯示內容 * @return */ public static Bitmap creatBarcode(Context context, String contents, int desiredWidth, int desiredHeight, boolean displayCode) { Bitmap ruseltBitmap = null; /** * 圖片兩端所保留的空白的寬度 */ int marginW = 20; /** * 條形碼的編碼類型 */ BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128; if (displayCode) { Bitmap barcodeBitmap = encodeAsBitmap(contents, barcodeFormat, desiredWidth, desiredHeight); Bitmap codeBitmap = creatCodeBitmap(contents, desiredWidth + 2 * marginW, desiredHeight, context); ruseltBitmap = mixtureBitmap(barcodeBitmap, codeBitmap, new PointF( 0, desiredHeight)); } else { ruseltBitmap = encodeAsBitmap(contents, barcodeFormat, desiredWidth, desiredHeight); } return ruseltBitmap; } /** * 生成條形碼的Bitmap * * @param contents * 需要生成的內容 * @param format * 編碼格式 * @param desiredWidth * @param desiredHeight * @return * @throws WriterException */ protected static Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int desiredWidth, int desiredHeight) { final int WHITE = 0xFFFFFFFF; final int BLACK = 0xFF000000; MultiFormatWriter writer = new MultiFormatWriter(); BitMatrix result = null; try { result = writer.encode(contents, format, desiredWidth, desiredHeight, null); } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } int width = result.getWidth(); int height = result.getHeight(); int[] pixels = new int[width * height]; // All are 0, or black, by default for (int y = 0; y < height; y++) { int offset = y * width; for (int x = 0; x < width; x++) { pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } /** * 生成顯示編碼的Bitmap * * @param contents * @param width * @param height * @param context * @return */ protected static Bitmap creatCodeBitmap(String contents, int width, int height, Context context) { TextView tv = new TextView(context); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); tv.setLayoutParams(layoutParams); tv.setText(contents); tv.setHeight(height); tv.setGravity(Gravity.CENTER_HORIZONTAL); tv.setWidth(width); tv.setDrawingCacheEnabled(true); tv.setTextColor(Color.BLACK); tv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight()); tv.buildDrawingCache(); Bitmap bitmapCode = tv.getDrawingCache(); return bitmapCode; } /** * 將兩個Bitmap合併成一個 * * @param first * @param second * @param fromPoint * 第二個Bitmap開始繪製的起始位置(相對於第一個Bitmap) * @return */ protected static Bitmap mixtureBitmap(Bitmap first, Bitmap second, PointF fromPoint) { if (first == null || second == null || fromPoint == null) { return null; } int marginW = 20; Bitmap newBitmap = Bitmap.createBitmap( first.getWidth() + second.getWidth() + marginW, first.getHeight() + second.getHeight(), Config.ARGB_4444); Canvas cv = new Canvas(newBitmap); cv.drawBitmap(first, marginW, 0, null); cv.drawBitmap(second, fromPoint.x, fromPoint.y, null); cv.save(Canvas.ALL_SAVE_FLAG); cv.restore(); return newBitmap; }CaptureActivity.java是掃描二維碼和條形碼的界面,對Camera進行初始化和開啓掃描線程
private void initCamera(SurfaceHolder surfaceHolder) { if (surfaceHolder == null) { throw new IllegalStateException("No SurfaceHolder provided"); } if (cameraManager.isOpen()) { ZXingApplication .print_i("CaptureActivity", "initCamera() while already open -- late SurfaceView callback?"); return; } try { cameraManager.openDriver(surfaceHolder); } catch (IOException ioe) { return; } catch (RuntimeException e) { return; } if (handler == null) { handler = new CaptureActivityHandler(this, decodeFormats, decodeHints, characterSet, cameraManager); } ZXingApplication.print_i("CaptureActivity", "initCamera-----------finish"); }在CaptureActivityHandler.java類中會處理掃描的結果
@Override public void handleMessage(Message message) { switch (message.what) { case R.id.restart_preview: restartPreviewAndDecode(); break; case R.id.decode_succeeded: state = State.SUCCESS; Bundle bundle = message.getData(); Bitmap barcode = null; float scaleFactor = 1.0f; if (bundle != null) { byte[] compressedBitmap = bundle .getByteArray(DecodeThread.BARCODE_BITMAP); if (compressedBitmap != null) { barcode = BitmapFactory.decodeByteArray(compressedBitmap, 0, compressedBitmap.length, null); // Mutable copy: barcode = barcode.copy(Bitmap.Config.ARGB_8888, true); } scaleFactor = bundle .getFloat(DecodeThread.BARCODE_SCALED_FACTOR); } activity.handleDecode((Result) message.obj, barcode, scaleFactor); break; case R.id.decode_failed: // We're decoding as fast as possible, so when one decode fails, // start another. state = State.PREVIEW; cameraManager.requestPreviewFrame(decodeThread.getHandler(), R.id.decode); break; case R.id.return_scan_result: activity.setResult(Activity.RESULT_OK, (Intent) message.obj); activity.finish(); break; case R.id.launch_product_query: String url = (String) message.obj; Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); intent.setData(Uri.parse(url)); ResolveInfo resolveInfo = activity.getPackageManager() .resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); String browserPackageName = null; if (resolveInfo != null && resolveInfo.activityInfo != null) { browserPackageName = resolveInfo.activityInfo.packageName; Log.d(TAG, "Using browser in package " + browserPackageName); } // Needed for default Android browser / Chrome only apparently if ("com.android.browser".equals(browserPackageName) || "com.android.chrome".equals(browserPackageName)) { intent.setPackage(browserPackageName); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(Browser.EXTRA_APPLICATION_ID, browserPackageName); } try { activity.startActivity(intent); } catch (ActivityNotFoundException ignored) { Log.w(TAG, "Can't find anything to handle VIEW of URI " + url); } break; } }在case R.id.decode_succeeded 分支會經過activity.handleDecode((Result) message.obj, barcode, scaleFactor);回傳結果給Activity
/** * A valid barcode has been found, so give an indication of success and show * the results. * * @param rawResult * The contents of the barcode. * @param scaleFactor * amount by which thumbnail was scaled * @param barcode * A greyscale bitmap of the camera data which was decoded. */ public void handleDecode(final Result rawResult, Bitmap barcode, float scaleFactor) { inactivityTimer.onActivity(); // ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler( // this, rawResult); boolean fromLiveScan = barcode != null; AlertDialog.Builder dialog = new AlertDialog.Builder(this); if (barcode == null) { dialog.setIcon(null); } else { Drawable drawable = new BitmapDrawable(barcode); dialog.setIcon(drawable); } dialog.setTitle("掃描結果"); dialog.setMessage(rawResult.getText()); dialog.setNegativeButton("肯定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 用默認瀏覽器打開掃描獲得的地址 Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); Uri content_url = Uri.parse(rawResult.getText()); intent.setData(content_url); startActivity(intent); finish(); } }); dialog.setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); dialog.create().show(); }這裏僅僅是把掃描的結果顯示出來,實際上可以依據不一樣的需要來處理,假設掃描出二維碼可以用HTTP來打開,有一點要注意的,個人ADT版本號是V23.0.2.1259578,需要執行的4.0的系統上