近期的一个手表项目需要用到二维码生成,于是就研究了一下。实现起来很简便,将jar包放入工程,就可以直接使用API了。
设备将位置信息更新给服务器,在浏览器输入URL访问指定ID设备的位置信息。这个URL可以用二维码的形式展示出来。
1.生成二维码:
private void initialLayout() {
ImageView imageQRCode = (ImageView) findViewById(R.id.imageQRCode);
String contentQRCode = Constant.Server.URL_MAP_INDEX + MyApp.deviceId;
try {
// 根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(310*310)
Bitmap bitmapQRCode = QRCodeUtil.createQRCode(contentQRCode,
310);
imageQRCode.setImageBitmap(bitmapQRCode);
} catch (WriterException e) {
e.printStackTrace();
}
}
QRCodeUtil:
import java.util.Hashtable;
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;
public final class QRCodeUtil {
private static final int BLACK = 0xff000000;
public static Bitmap createQRCode(String str, int widthAndHeight)
throws WriterException {
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
}
效果图:
可以用微信扫一扫,也可以用自己写的解析程序扫描,进入指定的URL。
2.生成条形码
条形码有很多种类,二维(条形)码就是其中一种。表示内容也有不同,有的只能表示纯数字,不能表示字母。
BarcodeFormat.CODE_128; // 表示高密度数据, 字符串可变长,符号内含校验码
BarcodeFormat.CODE_39;
BarcodeFormat.CODE_93;
BarcodeFormat.CODABAR; // 可表示数字0 - 9,字符$、+、 -、还有只能用作起始/终止符的a,b,c d四个字符,可变长度,没有校验位
BarcodeFormat.DATA_MATRIX;
BarcodeFormat.EAN_8;
BarcodeFormat.EAN_13;
BarcodeFormat.ITF;
BarcodeFormat.PDF417; // 二维码
BarcodeFormat.QR_CODE; // 二维码
BarcodeFormat.RSS_EXPANDED;
BarcodeFormat.RSS14;
BarcodeFormat.UPC_E; // 统一产品代码E:7位数字,最后一位为校验位
BarcodeFormat.UPC_A; // 统一产品代码A:12位数字,最后一位为校验位
BarcodeFormat.UPC_EAN_EXTENSION;
ublic static Bitmap creatBarcode(Context context, String contents,
int desiredWidth, int desiredHeight, boolean displayCode) {
Bitmap ruseltBitmap = null;
int marginW = 20;
BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;
if (displayCode) {
Bitmap barcodeBitmap = encodeAsBitmap(contents, barcodeFormat,
desiredWidth, desiredHeight);
Bitmap codeBitmap = creatCodeBitmap(contents, desiredWidth + 2
- marginW, desiredHeight, context);
ruseltBitmap = mixtureBitmap(barcodeBitmap, codeBitmap, new PointF(
0, desiredHeight));
} else {
ruseltBitmap = encodeAsBitmap(contents, barcodeFormat,
desiredWidth, desiredHeight);
}
return ruseltBitmap;
}
protected 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 {
result = writer.encode(contents, format, desiredWidth,
desiredHeight, null);
} 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;
}
protected static Bitmap creatCodeBitmap(String contents, int width,
int height, Context context) {
TextView tv = new TextView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(layoutParams);
tv.setText(contents);
tv.setHeight(height);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setWidth(width);
tv.setDrawingCacheEnabled(true);
tv.setTextColor(Color.BLACK);
tv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
tv.buildDrawingCache();
Bitmap bitmapCode = tv.getDrawingCache();
return bitmapCode;
}
最后
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长。而不成体系的学习效果低效漫长且无助。时间久了,付出巨大的时间成本和努力,没有看到应有的效果,会气馁是再正常不过的。
所以学习一定要找到最适合自己的方式,有一个思路方法,不然不止浪费时间,更可能把未来发展都一起耽误了。
如果你是卡在缺少学习资源的瓶颈上,那么刚刚好我能帮到你。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!