android QRCode

1.加载静态库和初始化 ImageScanner.

static {
		System.loadLibrary("iconv");
	}


// net.sourceforge.zbar.Config
		mImageScanner = new ImageScanner();
		mImageScanner.setConfig(0, Config.X_DENSITY, 3);
		mImageScanner.setConfig(0, Config.Y_DENSITY, 3);

 2.打开Camera设置previewcallback.

   

try {
			mCamera.setPreviewDisplay(mSurfaceHolder);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mCamera.setPreviewCallback(previewCallback);
		mCamera.startPreview();

 3.定义previewcallback.

  

public Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {

		@Override
		public void onPreviewFrame(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			if (!isPass) {
				Camera.Parameters parameters = camera.getParameters();
				Size size = parameters.getPreviewSize();

				Image barcode = new Image(size.width, size.height, "Y800");
				barcode.setData(data);

				int result = mImageScanner.scanImage(barcode);

				if (result != 0) {
					String mDecodeQRCode = "";
					SymbolSet syms = mImageScanner.getResults();
					for (Symbol sym : syms) {
						mDecodeQRCode = sym.getData();
						Log.i("W", "HDMI_Decode--> " + mDecodeQRCode);
						Toast.makeText(MainActivity.this, "QRCode: " + mDecodeQRCode, Toast.LENGTH_SHORT).show();
						if (mDecodeQRCode != null && !isPass) {
							// if (mDecodeQRCode.equals(mCurQR)) {
							resetCamera();
							isPass = true;
							//playSound();
							// }
						}
						break;
					}
				}
			}
		}
	};

 

   程序需要用到Zbar.jar。

你可能感兴趣的:(android)