温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android如何生成条形码和二维码功能

发布时间:2021-09-26 17:12:58 来源:亿速云 阅读:151 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关Android如何生成条形码和二维码功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

背景:

随着移动互联网的普及以及智能终端设备的广泛应用,移动支付变得越来越便捷,通过扫描二维码代替传统的刷卡行为。那么作为开发者而言生成二维码成为了一项必备技能。

准备:

使用zxing包

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

核心代码:

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 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, 0, width, 0, 0, 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, 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, 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;  }}

使用:

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

<?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>

感谢各位的阅读!关于“Android如何生成条形码和二维码功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI