Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate Bar Code and QR Code function in Android

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how Android generates barcodes and QR codes. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Background:

With the popularity of the mobile Internet and the wide application of intelligent terminal devices, mobile payment is becoming more and more convenient, by scanning the QR code instead of the traditional credit card behavior. So as a developer, generating a QR code has become a necessary skill.

Prepare:

Use the zxing package

Implementation "com.google.zxing:core:3.3.1"

Core code:

Package com.wangpengpro.h6test.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 15:05 on 2019-10-10. * usage: * / public class CodeUtils {/ * generate barcodes (not supported in Chinese) * * @ 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; yearly +) {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, 0, width, 0, 0, width, height) Return bitmap;} catch (WriterException e) {e.printStackTrace ();} return null;} / * * generate the QR code * * @ param content * @ return * / public static Bitmap createQrcode (String content) {Map hints = new HashMap (); / / support Chinese configuration 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, 1000, hints); int width = bitMatrix.getWidth (); int height = bitMatrix.getHeight (); int [] pixels = new int [width * height]; for (int y = 0; y < height; yearly +) {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, 0, width, 0,0, width, height); return bitmap;} catch (WriterException e) {e.printStackTrace ();} return null }}

Use:

ImageActivity.javapublic 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

Thank you for reading! This is the end of the article on "how to generate bar code and QR code function of Android". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report