适用于Android的条形码/ Qr代码阅读器

前端之家收集整理的这篇文章主要介绍了适用于Android的条形码/ Qr代码阅读器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的应用程序中实现QR Code / Barcode阅读器.我想知道什么是最轻量级的解决方案(无视zxing的意图集成商).

解决方法

我用zxing构建了我的应用程序.你需要一些编码.首先包括core.jar,它在core / core.jar,在你的构建路径中,然后转到他们的客户端,在 android /…./ com.google.zxing,并获取他们的代码(这不是由开发者,因为你的副本和粘贴.)最后,添加代码
package com.wtsang02.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
import com.google.zxing.common.HybridBinarizer;


public class QRDecoder extends Activity implements OnClickListener {

    private String text;
    private Button webbutton;
    private Bitmap bmp;
    private ImageView ivPicture;
    private TextView textv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mysales);
        webbutton = (Button)findViewById(R.id.webbutton);

        ivPicture = (ImageView) findViewById(R.id.ivPicture);
        textv= (TextView) findViewById(R.id.mytext);

        webbutton.setOnClickListener(this);
    }

    private void decode() {


        if (bmp == null) {
            Log.i("tag","wtf");
        }
        bmp = bmp.copy(Bitmap.Config.ARGB_8888,true);

        int[] intArray = new int[bmp.getWidth() * bmp.getHeight()];
        bmp.getPixels(intArray,bmp.getWidth(),bmp.getHeight());

        LuminanceSource source = new com.google.zxing.RGBLuminanceSource(
                bmp.getWidth(),bmp.getHeight(),intArray);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Reader reader = new MultiFormatReader();
        try {
            Result result = reader.decode(bitmap);

            text = result.getText();
            byte[] rawBytes = result.getRawBytes();
            BarcodeFormat format = result.getBarcodeFormat();
            ResultPoint[] points = result.getResultPoints();
            textv.setText(text);

        } catch (NotFoundException e) {

            e.printStackTrace();
        } catch (ChecksumException e) {

            e.printStackTrace();
        } catch (FormatException e) {

            e.printStackTrace();

        }
        Log.i("done","done");
        if(text!=null)
        Toast.makeText(getBaseContext(),text,Toast.LENGTH_LONG).show();
        else{
            Toast.makeText(getBaseContext(),"QQ",Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onClick(View v) {

        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i,0);
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) extras.get("data");
            ivPicture.setImageBitmap(bmp);
            decode();
        }

    }

}

代码将使用您手机的默认摄像头,如果您需要使用他们的客户端,您将需要启动他们的CaptureActivity,您的布局应包括TextView以显示结果,ImageView显示您捕获的图像,以及按钮以启动摄像头. .这是基于2.1zxing.

猜你在找的Android相关文章