参考慕课网
主要使用到谷歌官方支持的zxing开源包
zxing开源地址:https://github.com/zxing/zxing
但是由于里面不需要的东西太多,很多开发者对其在安卓方面的api进行了抽取,这里我使用了徐宜生所抽取的开源包
徐宜生: https://github.com//xuyisheng/ZXingLib
效果图
工程目录结构
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="二维码扫描"
android:layout_gravity="center"
android:textSize="16sp"
android:textColor="#ff0000"
android:gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开启扫描"
android:onClick="scan"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示扫描结果"
android:textSize="16sp"/>
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="二维码制作"
android:layout_gravity="center"
android:textSize="16sp"
android:textColor="#ff0000"
android:gravity="center"
android:layout_marginTop="60dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入文本"/>
<EditText
android:id="@+id/et_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<CheckBox
android:id="@+id/cb_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="logo"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="创建二维码"
android:onClick="create" />
<ImageView
android:id="@+id/iv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@mipmap/ic_launcher"
/>
</LinearLayout>
主界面
package com.android.qrcodetest;
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.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 MainActivity extends AppCompatActivity {
private TextView mTextResult;
private EditText mInput;
private CheckBox mLogo;
private ImageView mImageResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextResult = (TextView) findViewById(R.id.tv_result);
mLogo = (CheckBox) findViewById(R.id.cb_logo);
mInput = (EditText) findViewById(R.id.et_text);
mImageResult = (ImageView) findViewById(R.id.iv_result);
}
//扫描二维码
public void scan(View view){
startActivityForResult(new Intent(MainActivity.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();
String result = bundle.getString("result");
mTextResult.setText(result);
}
}
//创建二维码
public void create(View view){
String input = mInput.getText().toString();
if(input.equals("")){
Toast.makeText(MainActivity.this,"输入不能为空",Toast.LENGTH_SHORT).show();
}else{
Bitmap bitmap = EncodingUtils.createQRCode(input,500,500,mLogo.isChecked() ? BitmapFactory.decodeResource(getResources(),R.drawable.logo) : null);
mImageResult.setImageBitmap(bitmap);
}
}
}