在这之前,zxing项目还不知道如何下载、如何运行官方demo的同学请跳到
http://blog.csdn.net/quwei3930921/article/details/51206245
现在我们就来说说如何利用zxing核心库生成二维码、条码。
1.首先我们在官方demo的QRCodeEncoder.java中发现了以下方法:
Bitmap encodeAsBitmap() throws WriterException {
String contentsToEncode = contents;
if (contentsToEncode == null) {
return null;
}
Map<EncodeHintType,Object> hints = null;
String encoding = guessAppropriateEncoding(contentsToEncode);
if (encoding != null) {
hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
BitMatrix result;
try {
result = new MultiFormatWriter().encode(contentsToEncode, format, dimension, dimension, hints);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int width = result.getWidth();
int height = result.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] = 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的主要代码。
2.将其移植出来加以更改如下:
/**
* @param contents 要转换成二维码、条码的内容
* @param format 枚举BarcodeFormat中的值,表示要转换成什么图,二维码、条码
* @param desWidth 目标图的宽
* @param desHeight 目标图的高
* @return 二维码、条码图
* @throws WriterException 发生异常
*/
public Bitmap encodeAsBitmap(String contents, com.google.zxing.BarcodeFormat format,
int desWidth, int desHeight) throws WriterException {
String contentsToEncode = contents;
if (contentsToEncode == null) {
return null;
}
Map<EncodeHintType, Object> hints = null;
String encoding = guessAppropriateEncoding(contentsToEncode);
if (encoding != null) {
hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
BitMatrix result;
try {
result = new MultiFormatWriter().encode(contentsToEncode, format, desWidth, desHeight, hints);
} catch (IllegalArgumentException iae) {
// Unsupported format
return null;
}
int width = result.getWidth();
int height = result.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] = result.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;
}
private static String guessAppropriateEncoding(CharSequence contents) {
// Very crude at the moment
for (int i = 0; i < contents.length(); i++) {
if (contents.charAt(i) > 0xFF) {
return "UTF-8";
}
}
return null;
}
3.如何使用?
EditText contentsEdit;
Button encodeBtn;
ImageView qrcodeImgv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contentsEdit = (EditText) findViewById(R.id.contents);
encodeBtn = (Button) findViewById(R.id.encode);
encodeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String contents = contentsEdit.getText().toString().trim();
if (TextUtils.isEmpty(contents))
return;
try {
Bitmap bitmap = encodeAsBitmap(contents, BarcodeFormat.QR_CODE, 300, 300);
qrcodeImgv.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
}
});
qrcodeImgv = (ImageView) findViewById(R.id.qr_code);
}
没什么难度,只需要注意一下BarcodeFormat,一个枚举,表示要转换成什么图,二维码、条码,有如下类型可选择:
AZTEC,
CODABAR,
CODE_39,
CODE_93,
CODE_128, // 条码
DATA_MATRIX,
EAN_8,
EAN_13,
ITF,
MAXICODE,
PDF_417,
QR_CODE, // 二维码
RSS_14,
RSS_EXPANDED,
UPC_A,
UPC_E,
UPC_EAN_EXTENSION;
自己百度下对应生成时何种码吧。
总结,生成二维码比扫描要简单很多,所以同学们直接复制拿走吧。