跟着别人学滴,特此记录一下。
1.添加依赖:
compile 'com.google.zxing:core:3.3.0'
2.布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:gravity="center"
android:layout_gravity="center_horizontal">
<EditText
android:id="@+id/ed_code_text"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入条码内容"/>
<Button
android:id="@+id/btn_bar_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生成一维条码"/>
<Button
android:id="@+id/btn_qr_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生成二维码"/>
</LinearLayout>
<ImageView
android:id="@+id/img_code"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</RelativeLayout>
3.activity代码:
public class HomeActivity extends AppCompatActivity {
@BindView(R.id.ed_code_text)
EditText edCodeText;
@BindView(R.id.btn_bar_code)
Button btnBarCode;
@BindView(R.id.btn_qr_code)
Button btnQrCode;
@BindView(R.id.img_code)
ImageView imgCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
}
/**
* 制作二维码
*/
public void makeQRCode(String qrCodeText){
Bitmap bmp = null;
try {
if (!TextUtils.isEmpty(qrCodeText)) {
bmp = CreateTwoDCode(qrCodeText);
}
} catch (WriterException e) {
e.printStackTrace();
}
if (bmp != null) {
imgCode.setImageBitmap(bmp);
}
}
/**
* 制作条形码
*/
public void makeBarCode(String barCode){
int size = barCode.length();
for (int i = 0; i < size; i++) {
int c = barCode.charAt(i);
if ((19968 <= c && c < 40623)) {
Toast.makeText(HomeActivity.this, "生成条形码的时刻不能是中文", Toast.LENGTH_SHORT).show();
return;
}
}
Bitmap bmp = null;
try {
if (!TextUtils.isEmpty(barCode)) {
bmp = CreateOneDCode(barCode);
}
} catch (WriterException e) {
e.printStackTrace();
}
if (bmp != null) {
imgCode.setImageBitmap(bmp);
}
}
/**
* 将指定的内容生成二维码
*
* @param content 将要生成二维码的内容
* @return 返回生成好的二维码事件
* @throws WriterException WriterException异常
*/
public Bitmap CreateTwoDCode(String content) throws WriterException {
// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
int imgWidth = this.getResources().getDisplayMetrics().widthPixels / 2;
int imgHeight = imgWidth ;
BitMatrix matrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, imgWidth, imgHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
// 二维矩阵转为一维像素数组,也就是一直横着排了
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
// 通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
/**
* 用于将给定的内容生成成一维条码 注:目前生成内容为中文的话将直接报错,要修改底层jar包的内容
*
* @param content 将要生成一维条码的内容
* @return 返回生成好的一维条码bitmap
* @throws WriterException WriterException异常
*/
public Bitmap CreateOneDCode(String content) throws WriterException {
// 生成一维条码,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
int imgWidth = this.getResources().getDisplayMetrics().widthPixels - 40;
int imgHeight = imgWidth / 5 * 2;
BitMatrix matrix = new MultiFormatWriter().encode(content,
BarcodeFormat.CODE_128, imgWidth, imgHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
// 通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
@OnClick({R.id.btn_bar_code, R.id.btn_qr_code})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_bar_code:
String barCodeText = edCodeText.getText().toString().trim();
makeBarCode(barCodeText);//二维码
break;
case R.id.btn_qr_code:
String qrCodeText = edCodeText.getText().toString().trim();
makeQRCode(qrCodeText);//一维条码
break;
}
}
}