当前位置: 代码迷 >> Android >> Android生成跟扫描二维码之ZXing
  详细解决方案

Android生成跟扫描二维码之ZXing

热度:99   发布时间:2016-04-28 06:58:19.0
Android生成和扫描二维码之ZXing
ZXing是一个开源的条码生成和扫描库。它不但支持众多的条码格式,而且有各种语言的实现版本,它支持的语言包括:Java、 C++、 C#、 Objective-C、ActionScript以及Ruby。

http://code.google.com/p/zxing/downloads/list
版本:ZXing-2.3.0

(1)把ZXing的以下两个jar包放入android工程的libs下
引用
core/core.jar
javase/javase.jar


(2)生成二维码
public class QRCodeGenerateActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_qrcode_generate);        Button button = (Button) findViewById(R.id.generate_button);        button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                EditText editText = (EditText) findViewById(R.id.code_content);                Bitmap qrcode = generateQRCode(editText.getText().toString());                ImageView imageView = (ImageView) findViewById(R.id.code_image);                imageView.setImageBitmap(qrcode);            }        });    }    private Bitmap bitMatrix2Bitmap(BitMatrix matrix) {        int w = matrix.getWidth();        int h = matrix.getHeight();        int[] rawData = new int[w * h];        for (int i = 0; i < w; i++) {            for (int j = 0; j < h; j++) {                int color = Color.WHITE;                if (matrix.get(i, j)) {                    color = Color.BLACK;                }                rawData[i + (j * w)] = color;            }        }        Bitmap bitmap = Bitmap.createBitmap(w, h, Config.RGB_565);        bitmap.setPixels(rawData, 0, w, 0, 0, w, h);        return bitmap;    }    private Bitmap generateQRCode(String content) {        try {        	QRCodeWriter writer = new QRCodeWriter();            // MultiFormatWriter writer = new MultiFormatWriter();            BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 500, 500);            return bitMatrix2Bitmap(matrix);        } catch (WriterException e) {            e.printStackTrace();        }        return null;    }}



(3)扫描二维码
private PreviewCallback mPreviewCallback = new PreviewCallback() {    @Override    public void onPreviewFrame(byte[] data, Camera camera) {        int previewWidth = camera.getParameters().getPreviewSize().width;        int previewHeight = camera.getParameters().getPreviewSize().height;        PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(                data, previewWidth, previewHeight, 0, 0, previewWidth,                previewHeight, false);        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));        Reader reader = new QRCodeReader();        // Reader reader = new MultiFormatReader();        try {            Result result = reader.decode(bitmap);            String text = result.getText();            Intent intent = new Intent();            intent.setData(Uri.parse(text));            setResult(RESULT_OK, intent);            finish();        } catch (Exception e) {            e.printStackTrace();            Toast.makeText(getApplicationContext(), "Not Found", Toast.LENGTH_SHORT).show();        }    }};

  相关解决方案