需要用到的jar包:zxing3.3.jar
下載地址:https://pan.baidu.com/s/1UTazz8raimOkB8VTCyLQMQ 提取碼: sc5s
下載后,放入項目的libs目錄下,如圖:

圖1.png
補(bǔ)充:如果你看到的是下面這種,點擊Android切換到Project即可

圖2.png
右擊zxing3.3.jar文件,選擇Add As Library:

圖3.png
將下列代碼根據(jù)需要放入指定地方
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);//通過字符串創(chuàng)建二維矩陣
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;//根據(jù)二維矩陣數(shù)據(jù)創(chuàng)建數(shù)組
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//創(chuàng)建位圖
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);//將數(shù)組加載到位圖中
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,"內(nèi)容不能為空!",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);//通過字符串創(chuàng)建二維矩陣
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;//根據(jù)二維矩陣數(shù)據(jù)創(chuàng)建數(shù)組
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//創(chuàng)建位圖
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);//設(shè)置位圖像素集為數(shù)組
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;
}
}
歡迎各位大牛指正^ _ ^