Android开发二维码扫描功能集成开发

如果看代码不明白也可以下载视频例子查看

这个开源项目可以扫描一维,和二维码,
一维码指的是书后面的条形码
首先配置ZXing库和Android工程
项目本身非常大,我们只需使用精简版的库
导入之后


Android开发二维码扫描功能集成开发_第1张图片
Android开发二维码扫描功能集成开发_第2张图片

TestQRcode 项目作为库被其他项目使用。
BarCodeTestActivity

package com.ericssonlabs;

import com.google.zxing.WriterException;
import com.zxing.activity.CaptureActivity;
import com.zxing.encoding.EncodingHandler;

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

public class BarCodeTestActivity extends Activity {
    /** Called when the activity is first created. */
    private TextView resultTextView;
    private EditText qrStrEditText;
    private ImageView qrImgImageView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        resultTextView = (TextView) this.findViewById(R.id.tv_scan_result);
        qrStrEditText = (EditText) this.findViewById(R.id.et_qr_string);
        qrImgImageView = (ImageView) this.findViewById(R.id.iv_qr_image);
        
        Button scanBarCodeButton = (Button) this.findViewById(R.id.btn_scan_barcode);
        scanBarCodeButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                //打开扫描界面扫描条形码或二维码
                Intent openCameraIntent = new Intent(BarCodeTestActivity.this,CaptureActivity.class);
                startActivityForResult(openCameraIntent, 0);
            }
        });
        
        Button generateQRCodeButton = (Button) this.findViewById(R.id.btn_add_qrcode);
        generateQRCodeButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                try {
                    String contentString = qrStrEditText.getText().toString();
                    if (!contentString.equals("")) {
                        //根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(350*350)
                        Bitmap qrCodeBitmap = EncodingHandler.createQRCode(contentString, 350);
                        qrImgImageView.setImageBitmap(qrCodeBitmap);
                    }else {
                        Toast.makeText(BarCodeTestActivity.this, "Text can not be empty", Toast.LENGTH_SHORT).show();
                    }
                    
                } catch (WriterException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //处理扫描结果(在界面上显示)
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            resultTextView.setText(scanResult);
        }
    }
}

创建项目TestQRcode,来使用BarCodeTest库
xml文件



    

MainActivity

package com.jikexueyuan.testqrcode;

import junit.framework.Test;

import com.google.zxing.WriterException;
import com.zxing.activity.CaptureActivity;
import com.zxing.encoding.EncodingHandler;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity {

    private Button scanButton;
    private TextView text;
    private EditText input;
    private Button genButton;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scanButton = (Button) findViewById(R.id.scan);
        text = (TextView) findViewById(R.id.text);
        scanButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Toast.makeText(MainActivity.this, "你写可以扫描条形码或者二维码",
                        Toast.LENGTH_SHORT).show();
                Intent startScan = new Intent(MainActivity.this,
                        CaptureActivity.class);
                // startActivity(startScan);
                startActivityForResult(startScan, 0);
            }
        });

        input = (EditText) findViewById(R.id.input);
        genButton = (Button) findViewById(R.id.gen);
        img = (ImageView) findViewById(R.id.img);
        genButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String in = input.getText().toString();
                if (in.equals("")) {
                    Toast.makeText(MainActivity.this, "请输入文本",
                            Toast.LENGTH_SHORT).show();
                } else {
                    try {

                        Bitmap qrcode = EncodingHandler.createQRCode(in, 400);
                        img.setImageBitmap(qrcode);
                    } catch (WriterException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            String result = data.getExtras().getString("result");
            text.setText(result);
        }
    }
}

注册CaptureActivity.java



另外还要添加两个权限,一个是振动,一个是照相机



源码下载地址:

链接:http://pan.baidu.com/s/1sle8mlR

视频下载地址:

链接:http://pan.baidu.com/s/1sle8mlR
关注公众号(ID:daimajiqiao)回复(二维码扫描)即可获取提取密码
当然我也会把所有Android开发技术视频教程统一返回给你。
付出总会有收获

你可能感兴趣的:(Android开发二维码扫描功能集成开发)