扫描生成二维码--附Demo

最简单的扫描生成二维码

ONE Goal,ONE Passion!


现在的app扫描二维码几乎就是标配,而且扫描二维码的demo是多如牛毛.可是几乎都是大篇的copy许多类.而且使用起来很是繁琐.我就是深受其害.每次都要搞半天.而且有时还不能使用.今天弄了一下午,终于简化了一个我个人认为目前我见到过的最最简单的demo.

  
  
  • 1
  • 2

第1步:

准备工作—–当然了,ZXing还是必须的.只是这是个简化了的Zxing.jar.

第2步:

这也是核心代码:
a.此activity就是描述了扫描工程以及处理扫描结果.

public class ScannerActivity extends Activity implements OnDecodeCompletionListener {

    private ScannerView scannerView;
    private String string;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        scannerView = (ScannerView) findViewById(R.id.scanner_view);
        scannerView.setOnDecodeListener(this);
    }


    @Override
    public void onDecodeCompletion(String barcodeFormat, String barcode, Bitmap bitmap) {
        if (barcode == null || "".equals(barcode)) {

            AlertDialog builder = new AlertDialog.Builder(ScannerActivity.this).
                    setTitle("Error").setMessage("Not Found").show();
        } else {
            string = barcode.substring(barcode.indexOf("?") + 1, barcode.length());
            mShowDialog(string);
        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        scannerView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        scannerView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**/

    private void mShowDialog(final String url) {
        AlertDialog alertDialog = new AlertDialog.Builder(ScannerActivity.this).setTitle("The Result")
                .setMessage(url).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }

                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (url.substring(0, 4).equals("http")) {
                            OpenURL(url);

                        } else {

                        }
                    }

                }).show();
    }

    private void OpenURL (String url) {
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
        finish();
    }
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

b.使用的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.scanner2.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <com.covics.zxingscanner.ScannerView
            android:id="@+id/scanner_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </FrameLayout>
</RelativeLayout>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

第2步:

在mainActivity中调用运行查看:

public class MainActivity extends Activity {

    Button scanner_qr, creater_qr;
    ImageView iv;
    EditText et_qr;
    Activity act;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_scanner);
        act = this;
        initView();

    }

    private void initView() {

        scanner_qr = (Button) findViewById(R.id.scanner_qr);
        et_qr = (EditText) findViewById(R.id.et_qr);
        creater_qr = (Button) findViewById(R.id.creater_qr);
        iv = (ImageView) findViewById(R.id.iv);

        scanner_qr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent inten = new Intent(MainActivity.this, ScannerActivity.class);
                startActivity(inten);

            }
        });

        creater_qr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = et_qr.getText().toString().trim();
                if (str.isEmpty()) {
                    Toast.makeText(MainActivity.this, "没有输入字符串", Toast.LENGTH_SHORT).show();
                } else {
                    final Bitmap qrCodeBitmap = createQRCodeBitmap(str);

                    act.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //将图标的bitmap转化成BitmapDrawable
                            BitmapDrawable iconDrawable = new BitmapDrawable(qrCodeBitmap);
                            iv.setBackground(iconDrawable);
                        }
                    });
                }

            }
        });

    }


  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

生成二维码的方法 .直接调用此方法传入url参数即可生成二维码.

 /**
     *    创建QR二维码图片方法
     */
    private Bitmap createQRCodeBitmap(String url) {
        // 用于设置QR二维码参数
        Hashtable<EncodeHintType, Object> qrParam = new Hashtable<EncodeHintType, Object>();
        // 设置QR二维码的纠错级别——这里选择最高H级别
        qrParam.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 设置编码方式
        qrParam.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        // 设定二维码里面的内容,这里我采用我博客的地址
        //   String cont = "http://blog.csdn.net/fengltxx";

        // 生成QR二维码数据——这里只是得到一个由true和false组成的数组
        // 参数顺序分别为:编码内容url地址,编码类型,生成图片宽度,生成图片高度,设置参数
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url,
                    BarcodeFormat.QR_CODE, 180, 180, qrParam);

            // 开始利用二维码数据创建Bitmap图片,分别设为黑白两色
            int w = bitMatrix.getWidth();
            int h = bitMatrix.getHeight();
            int[] data = new int[w * h];

            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    if (bitMatrix.get(x, y))
                        data[y * w + x] = 0xff000000;// 黑色
                    else
                        data[y * w + x] = -1;// -1 相当于0xffffffff 白色
                }
            }

            // 创建一张bitmap图片,采用最高的效果显示
            Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            // 将上面的二维码颜色数组传入,生成图片颜色
            bitmap.setPixels(data, 0, w, 0, 0, w, h);
            return bitmap;
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }


}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

mainActivity的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.scanner2.ScannerActivity">

    <Button
        android:id="@+id/scanner_qr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="扫描二维码" />

    <EditText
        android:id="@+id/et_qr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入要生成二维码的字符串"/>

    <Button
        android:id="@+id/creater_qr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="生成二维码" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="二维码图像展示"/>

    <ImageView
        android:id="@+id/iv"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:background="#8ff4f4"
        />


</LinearLayout>

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

最后,不要忘记添加使用摄像头的权限
 <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

二维码生成扫描的demo案例已经全部实现了.Zxing包的内容有很多,用起来感觉不是太方便,简化了jar包后,使用起来还是很方便的.再也不用每次几百行代码去集成Zxing了.

下载Zxing.jar链接:http://download.csdn.net/my
demo下载:http://download.csdn.net/detail/fengltxx/9647285

转自PK_night的博客:http://blog.csdn.net/fengltxx/article/details/50404681

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值