您的位置:首页 > 移动开发 > Android开发

二维码的生成与扫描

2016-05-02 08:27 507 查看
最近公司项目里面用到了二维码,之前没有做过索性借着休息天好好学习,看了看Google推出的 Zxing 里面包含了很多,但是常用的 就是二维码生成与扫描,网上有很多大牛已经帮你整理好了第三方的库可以直接用的,最后我会上传我的依赖包也就是Zxing。

具体的实现方法:

1、将Zxing做为 Module导入到项目中



在mainifest中添加相机闪光灯的权限,否者打开相机会失败注意:

    <uses-permission android:name="android.permission.CAMERA" />

    <uses-permission android:name="android.permission.VIBRATE" />

    <uses-permission android:name="android.permission.FLASHLIGHT" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2、布局文件:

<?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="dadaapp.student.dadaxueche.com.zumeng.activity.QRCodeZxing">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Scan"
android:text="Start Scan"
android:textAllCaps="false" />

<TextView
android:id="@+id/main_tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result"
android:textAllCaps="false" />

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

<CheckBox
android:id="@+id/main_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Get"
android:text="Get"
android:textAllCaps="false" />

<ImageView
android:id="@+id/main_qrcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


 3、调用二维码扫描与生成



public class QRCodeZxing extends AppCompatActivity {
private TextView tv_result;
private CheckBox mCheck;
private EditText mEt;
private ImageView mImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrcode_zxing);
BrioalUtil.init(this);
tv_result = (TextView) findViewById(R.id.main_tv_result);
mCheck = (CheckBox) findViewById(R.id.main_check);
mEt = (EditText) findViewById(R.id.main_et);
mImage = (ImageView) findViewById(R.id.main_qrcode);
}

//打开二维码扫描页面
public void Scan(View view) {
//启动扫描的Activity 成功的返回码为 Result.OK
startActivityForResult(new Intent(QRCodeZxing.this, QRScanActivity.class), 0);
}

//产生二维码图片
public void Get(View view) {
if (mEt.getText().toString().equals("")) {
mEt.setError("输入不能为空");
return;
}
Window window = getWindow();
int mWidth = window.getWindowManager().getDefaultDisplay().getWidth();
if (mCheck.isChecked()) {
//生成带logo的二维码
QRCodeEncoder.encodeQRCode(mEt.getText().toString(),
mWidth,
getResources().getColor(R.color.colorAccent),
BitmapFactory.decodeResource(getResources(),
R.mipmap.logo), new QRCodeEncoder.Delegate() {
@Override
public void onEncodeQRCodeSuccess(Bitmap bitmap) {
mImage.setImageBitmap(bitmap);
}

@Override
public void onEncodeQRCodeFailure() {
Toast.makeText(QRCodeZxing.this, "生成二维码失败", Toast.LENGTH_SHORT).show();
}
});
} else {
//生成默认的二维码
QRCodeEncoder.encodeQRCode(mEt.getText().toString(), mWidth, getResources().getColor(R.color.colorAccent), new QRCodeEncoder.Delegate() {
@Override
public void onEncodeQRCodeSuccess(Bitmap bitmap) {
mImage.setImageBitmap(bitmap);
}

@Override
public void onEncodeQRCodeFailure() {
Toast.makeText(QRCodeZxing.this, "生成二维码失败", Toast.LENGTH_SHORT).show();
}
});
}

}

//接受扫描页面返回的信息
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
String result = bundle.getString("result");
tv_result.setText(result);
}
}
}


如果你想修改扫描二维码的界面布局可以到Module Zxing中的QRScanActivity.java中修改。大致的情况就是这样了,很简单的如果不满足需求可以去Google查看相关的文档。

Module Zxing 下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 二维码 谷歌