Android generates barcode and QR code
Background:
With the popularity of mobile Internet and the wide application of intelligent terminal devices, mobile payment becomes more and more convenient. Scanning QR code instead of traditional card swiping behavior. So as a developer, generating QR code has become a necessary skill.
get ready:
Using zxing package
implementation "com.google.zxing:core:3.3.1"
Core code:
package com.wangpengpro.h5test.utils; import android.graphics.Bitmap; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import java.util.HashMap; import java.util.Map; /** * @author Created by Mr.Wang on 2019/10/10 15:05. * usage: */ public class CodeUtils { /** * 生成条形码(不支持中文) * * @param content * @return */ public static Bitmap createBarcode(String content) { try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.CODE_128,3000,700); int width = bitMatrix.getWidth(); int height = bitMatrix.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] = bitMatrix.get(x,y) ? 0xff000000 : 0xFFFFFFFF; } } Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels,width,height); return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; } /** * 生成二维码 * * @param content * @return */ public static Bitmap createQrcode(String content) { Map<EncodeHintType,Object> hints = new HashMap<>(); // 支持中文配置 hints.put(EncodeHintType.CHARACTER_SET,"UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H); try { BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,1000,hints); int width = bitMatrix.getWidth(); int height = bitMatrix.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] = bitMatrix.get(x,height); return bitmap; } catch (WriterException e) { e.printStackTrace(); } return null; } }
use:
ImageActivity.java public class ImageActivity extends AppCompatActivity { @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image); ImageView ivBarcode = findViewById(R.id.iv_barcode); ImageView ivQrcode = findViewById(R.id.iv_qrcode); ivBarcode.setImageBitmap(CodeUtils.createBarcode("This is a barcode")); ivQrcode.setImageBitmap(CodeUtils.createQrcode("This is a qrcode")); } }
activity_ image.xml
<?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=".ImageActivity"> <ImageView android:id="@+id/iv_barcode" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv_qrcode" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Operation effect:
summary
The above is the Android barcode and QR code generation function introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!