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().方法名
太好用了记录下