码迷,mamicode.com
首页 > 其他好文 > 详细

二维码生成和扫描

时间:2016-06-30 21:44:57      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

 

 下载类库https://yunpan.cn/cBAZaMg5t2BXH  访问密码 5a55

 

1.项目外层的settings.gradle 中添加  

include ‘:App‘,‘:libzxing‘


2.libzxing的build.gradle中修改成和自己app相同的版本
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
minSdkVersion 15
targetSdkVersion 23


3.在 build.gradle 的
dependencies添加
compile project(‘:libzxing‘)



布局中
<?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=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="扫描二维码"
        android:id="@+id/btn_scan"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="scan" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="---显示扫描结果----"
        android:textSize="22dp"
        android:id="@+id/tv_ScanResult" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_Text"
        android:hint="这里请输入你要生成二维码的内容" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="二维码是否要包含图片Logo"
        android:id="@+id/cb_includeLogo"
        android:checked="false" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="生成二维码QRcode"
        android:id="@+id/btn_CreateQRcode"
        android:onClick="CreateQRode" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgv_QRcode"
        android:layout_gravity="center_horizontal"
        android:background="@mipmap/appicon" />

</LinearLayout>

activity中

package tests.bwie.com.mygridview;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.xys.libzxing.zxing.encoding.EncodingUtils;

public class ZxingActivity extends AppCompatActivity {
    private Button btn_scan;
    private TextView tv_scanResult;
    private EditText et_text;
    private CheckBox cb_include;
    private Button btn_createSRcode;
    private ImageView iv_qRcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zxing);
        //找控件
        findComp();
    }

    private void findComp() {
        btn_scan = (Button) findViewById(R.id.btn_scan);
        tv_scanResult = (TextView) findViewById(R.id.tv_ScanResult);
        et_text = (EditText) findViewById(R.id.et_Text);
        cb_include = (CheckBox) findViewById(R.id.cb_includeLogo);
        btn_createSRcode = (Button) findViewById(R.id.btn_CreateQRcode);
        iv_qRcode = (ImageView) findViewById(R.id.imgv_QRcode);
    }

    //直接调用第三方ZXing库的api进行扫描

    public void scan(View view){
        startActivityForResult(new Intent(ZxingActivity.this,
                CaptureActivity.class),0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode==RESULT_OK){
            Bundle bundle=data.getExtras();//通过Bundle获取扫描后的结果数据
            String sResult=bundle.getString("result");
            tv_scanResult.setText(sResult);
            System.out.println("查看读取值"+sResult);
        }

    }

    public void CreateQRode(View view){

        String sInput=et_text.getText().toString();
        if (sInput.equals("")){
            Toast.makeText(ZxingActivity.this,"还没输入要生成的二维码,请输入要生成二维码的内容",Toast.LENGTH_SHORT).show();
        }else{
            if (cb_include.isChecked()==false){
                //调用ZXing类库的createQRCode生成二维码----不包含图片
                Bitmap bitmap= EncodingUtils.createQRCode(sInput,500,500,null);
                iv_qRcode.setImageBitmap(bitmap);
            }else{
                //调用ZXing类库的createQRCode生成二维码----包含图片
                Bitmap bitmap= EncodingUtils.createQRCode(sInput,500,500,
                        BitmapFactory.decodeResource(getResources(),R.mipmap.appicon));
                iv_qRcode.setImageBitmap(bitmap);
            }
        }
    }
}

 

二维码生成和扫描

标签:

原文地址:http://www.cnblogs.com/gaoliangjie/p/5631155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!