需要用到的jar包:zxing3.3.jar
下载地址:https://pan.baidu.com/s/1StbJzjCuixD7Alpx0taQMA 提取码: x5yt
下载后,放入项目的libs目录下,如图:

补充:如果你看到的是下面这种,点击Android切换到Project即可

右击zxing3.3.jar文件,选择Add As Library:

将下列代码根据需要放入指定地方
try {
String str="哈哈哈";
Map<EncodeHintType, Object> hints = null;
String encoding = getEncoding(str);//获取字符编码
if (encoding != null) {
hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
BitMatrix result=new MultiFormatWriter().encode(str,BarcodeFormat.QR_CODE,350,350,hints);//通过字符串创建二维矩阵
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;//根据二维矩阵数据创建二维数组
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//创建位图
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);//设置位图像素集为二维数组
ivShow.setImageBitmap(bitmap);//显示二维码
} catch (WriterException e) {
e.printStackTrace();
}
获取编码的方法:
private static String getEncoding(CharSequence contents) {
// Very crude at the moment
for (int i = 0; i < contents.length(); i++) {
if (contents.charAt(i) > 0xFF) {
return "UTF-8";
}
}
return null;
}
最后贴出完整示例代码:
1、布局文件 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/et_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_encode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="生成二维码"/>
<ImageView
android:id="@+id/iv_show"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
2、java文件 MainActivity.java
public class MainActivity extends AppCompatActivity {
@BindView(R.id.et_content)
EditText etContent;
@BindView(R.id.iv_show)
ImageView ivShow;
private static final int WHITE = 0xFFFFFFFF;
private static final int BLACK = 0xFF000000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.btn_encode)
public void onViewClicked() {
String content=etContent.getText().toString();
if (content.equals("")){
Toast.makeText(MainActivity.this,"内容不能为空!",Toast.LENGTH_SHORT).show();
}else {
try {
Map<EncodeHintType, Object> hints = null;
String encoding = getEncoding(content);//获取编码格式
if (encoding != null) {
hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
BitMatrix result=new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,350,350,hints);//通过字符串创建二维矩阵
int width = result.getWidth();
int height = result.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;//根据二维矩阵数据创建数组
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//创建位图
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);//设置位图像素集为数组
ivShow.setImageBitmap(bitmap);//显示二维码
} catch (WriterException e) {
e.printStackTrace();
}
}
}
private static String getEncoding(CharSequence contents) {
// Very crude at the moment
for (int i = 0; i < contents.length(); i++) {
if (contents.charAt(i) > 0xFF) {
return "UTF-8";
}
}
return null;
}
}
欢迎各位大牛指正^ _ ^
网友评论