zxing生成各种码,可转bitMap,可旋转

package Utils;

import android.content.BroadcastReceiver;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.text.TextUtils;

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.QRCodeWriter;

import java.util.HashMap;
import java.util.Map;

import static com.google.zxing.BarcodeFormat.AZTEC;
import static com.google.zxing.BarcodeFormat.CODABAR;
import static com.google.zxing.BarcodeFormat.CODE_128;
import static com.google.zxing.BarcodeFormat.CODE_39;
import static com.google.zxing.BarcodeFormat.CODE_93;
import static com.google.zxing.BarcodeFormat.DATA_MATRIX;
import static com.google.zxing.BarcodeFormat.EAN_13;
import static com.google.zxing.BarcodeFormat.EAN_8;
import static com.google.zxing.BarcodeFormat.ITF;
import static com.google.zxing.BarcodeFormat.MAXICODE;
import static com.google.zxing.BarcodeFormat.PDF_417;
import static com.google.zxing.BarcodeFormat.QR_CODE;
import static com.google.zxing.BarcodeFormat.RSS_14;
import static com.google.zxing.BarcodeFormat.RSS_EXPANDED;
import static com.google.zxing.BarcodeFormat.UPC_A;
import static com.google.zxing.BarcodeFormat.UPC_E;
import static com.google.zxing.BarcodeFormat.UPC_EAN_EXTENSION;


public class QRCodeUtils {


    /**
     * 生成固定大小的二维码(不需网络权限)
     *
     * @param content 需要生成的内容
     * @param width   二维码宽度
     * @param height  二维码高度
     * @return
     */
    public static Bitmap createRQCode(String content, int width, int height) {
        //判断content是否为空
        if (TextUtils.isEmpty(content)) {
            return null;
        }

        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, "0");
        try {

            BitMatrix encode = qrCodeWriter.encode(content, QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    if (encode.get(j, i)) {
                        pixels[i * width + j] = 0x00000000;
                    } else {
                        pixels[i * width + j] = 0xffffffff;
                    }
                }
            }
            return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成条形码
     *
     * @param contents
     * @param desiredWidth
     * @param desiredHeight
     * @return
     */
    public static Bitmap createBarcode(String contents, int desiredWidth, int desiredHeight,int BarType) {
        if (TextUtils.isEmpty(contents)) {
            return null;
        }
        BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
        Bitmap resultBitmap = encodeAsBitmap(contents, barcodeFormat, desiredWidth, desiredHeight);

        return rotateBmp(resultBitmap, 0);
    }

    /**
     * 多种码制的生成
     * @param contents
     * @param format
     * @param desiredWidth
     * @param desiredHeight
     * @return
     */
    public static Bitmap encodeAsBitmap(String contents,
                                        BarcodeFormat format, int desiredWidth, int desiredHeight) {
        final int WHITE = 0xFFFFFFFF;
        final int BLACK = 0xFF000000;

        MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix result = null;
        try {
            Map<EncodeHintType, String> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            hints.put(EncodeHintType.MARGIN, "0");
            result = writer.encode(contents, format, desiredWidth, desiredHeight, hints);
        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int width = result.getWidth();
        int height = result.getHeight();
        int[] pixels = new int[width * height];
        // All are 0, or black, by default
        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;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    /**
     * 旋转bitmap
     *
     * @param b
     * @param degrees
     * @return
     */
    public static Bitmap rotateBmp(Bitmap b, int degrees) {
        if (degrees != 0 && b != null) {
            Matrix m = new Matrix();
            m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
            try {
                Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
                        b.getHeight(), m, true);
                if (b != b2) {
                    b.recycle();
                    b = b2;
                }
            } catch (OutOfMemoryError ex) {
            }
        }
        return b;
    }

    /**
     * 根据参数返回二维码格式
     * @param QRType
     * @return
     */
    private static BarcodeFormat QRTypeSwitch(int QRType){
        BarcodeFormat[]  barcodeFormats=new  BarcodeFormat[]{
                AZTEC, DATA_MATRIX,
                PDF_417,
                QR_CODE};
        return barcodeFormats[QRType%4];
    }
    /**
     * 根据参数返回一维码格式
     * @param BarType
     * @return
     */
    private static BarcodeFormat BarTypeSwitch(int BarType){
        BarcodeFormat[]  barcodeFormats=new  BarcodeFormat[]{
                CODABAR,
                CODE_39,
                CODE_93,
                CODE_128,
                EAN_8,
                EAN_13,
                ITF,
                RSS_14,
                RSS_EXPANDED,
                UPC_A,
                UPC_E,
                UPC_EAN_EXTENSION};

        return barcodeFormats[BarType%12];
    }
}

使用方法

new QRCodeUtils().方法名

太好用了记录下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android ZXing是一个用于生成和扫描二维的开源库。它提供了各种类型的二维生成功能。 首先,ZXing可以生成包含文本信息的普通二维。文本可以是网址、电话号、电子邮件地址等等。只需提供文本内容,ZXing就能生成一个可供扫描的二维。 其次,ZXing还支持生成包含WiFi网络信息的二维。你可以输入WiFi网络的名称、密和加密类型,ZXing生成一个二维,供其他设备通过扫描连接到该WiFi网络。 此外,ZXing可以生成带有联系人信息的二维。你可以输入联系人的姓名、电话号、电子邮件地址等信息,ZXing生成一个二维,其他人可以通过扫描该二维便捷地保存你的联系人信息。 另外一种类型的二维是地理位置二维。使用ZXing,你可以输入经度和纬度坐标,它将生成一个能够导航到指定位置的二维。 此外,ZXing还支持生成用于付款的二维,即支付二维,你可以输入付款金额和收款方信息,ZXing生成一个供其他人扫描用于付款的二维。 总之,ZXing是一个功能强大的库,可以生成各种类型的二维。无论是普通文本、WiFi网络信息、联系人信息、地理位置信息还是付款信息,通过ZXing,你可以方便地生成自定义的二维。 ### 回答2: Android中可以使用ZXing库来生成各种类型的二维ZXing是一个开源的二维扫描库,它提供了丰富的功能和灵活的API,可以方便地生成多种类型的二维。 在Android中使用ZXing生成二维非常简单。首先,需要在项目的gradle文件中添加ZXing库的依赖。然后,可以使用ZXing库提供的API来生成不同类型的二维。 例如,可以使用ZXing库的EncodeHintType参数来指定生成二维的类型。常见的类型包括URL、文本、联系人信息等。生成一个URL类型的二维可以使用如下代: ```java String url = "https://www.example.com"; // 要生成二维的URL地址 int width = 500; // 二维的宽度 int height = 500; // 二维的高度 Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix; try { bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, width, height, hints); } catch (WriterException e) { e.printStackTrace(); return; } int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { pixels[y * width + x] = bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE; } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); // 将生成的二维显示在ImageView中 imageView.setImageBitmap(bitmap); ``` 上述代首先创建一个Hashtable来设置生成二维的字符集,然后使用QRCodeWriter类中的encode方法生成BitMatrix对象,接着将BitMatrix化为Bitmap对象,并将Bitmap显示在ImageView中。 使用ZXing库可以轻松地生成各种类型的二维开发者可以按照需求进行配置和定制。这是Android开发中非常实用的一个功能。 ### 回答3: Android zxing可以用于生成多种类型的二维,其中包括URL链接、联系人信息、日历事件、地理位置、邮箱信息、短信消息、电话号和Wi-Fi网络连接等。 对于URL链接,使用zxing可以生成可以直接扫描打开网址的二维。只需将URL传递给zxing,它会生成对应的二维图片。 对于联系人信息,使用zxing可以将名字、电话号、邮箱地址等联系人信息编为二维。扫描这个二维后,用户可以直接将联系人信息保存到手机通讯录中。 对于日历事件,使用zxing可以将日程安排、会议时间等编为二维。扫描这个二维后,用户可以直接将日程事件添加到手机日历中。 对于地理位置,使用zxing可以将经纬度坐标编为二维。扫描这个二维后,用户可以直接导航到对应的地理位置。 对于邮箱信息,使用zxing可以将邮箱地址、主题、正文等编为二维。扫描这个二维后,用户可以直接发送邮件到指定邮箱。 对于短信消息,使用zxing可以将收件人号和消息内容编为二维。扫描这个二维后,用户可以直接发送短信。 对于电话号,使用zxing可以将电话号为二维。扫描这个二维后,用户可以直接拨打电话。 对于Wi-Fi网络连接,使用zxing可以将Wi-Fi名称、密和加密类型编为二维。扫描这个二维后,用户可以直接连接对应的Wi-Fi网络。 总之,zxing提供了方便快捷的方式来生成各种类型的二维,大大简化了在Android应用中生成二维的过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值