为Android生成具有高纠错能力的二维码
我目前正在我的Android应用程序中生成二维码,但客户抱怨我们需要生成二维码 我目前使用的代码如下,但问题是我不知道如何实际生成具有特定错误连接的QR图像:为Android生成具有高纠错能力的二维码,android,qr-code,zxing,Android,Qr Code,Zxing,我目前正在我的Android应用程序中生成二维码,但客户抱怨我们需要生成二维码 我目前使用的代码如下,但问题是我不知道如何实际生成具有特定错误连接的QR图像: private Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException { String contentsToEncode = contents;
private Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) 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, Object>(EncodeHintType.class);
hints.put(EncodeHintType.CHARACTER_SET, encoding);
}
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result;
try {
result = writer.encode(contentsToEncode, format, img_width, img_height, 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;
}
private Bitmap encodeAsBitmap(字符串内容、条形码格式、int-img\u宽度、int-img\u高度)引发WriterException{
字符串contentsToEncode=内容;
if(contentsToEncode==null){
返回null;
}
映射提示=null;
字符串编码=猜测适当编码(contentsToEncode);
if(编码!=null){
提示=新的EnumMap(EncodeHintType.class);
put(EncodeHintType.CHARACTER\u集,编码);
}
MultiFormatWriter writer=新的MultiFormatWriter();
位矩阵结果;
试一试{
结果=writer.encode(内容编码、格式、img\u宽度、img\u高度、提示);
}捕获(IllegalArgumentException iae){
//不支持的格式
返回null;
}
int width=result.getWidth();
int height=result.getHeight();
int[]像素=新int[宽度*高度];
对于(int y=0;y
提前谢谢你的帮助 如果您需要在线qrcode生成器,请使用谷歌图表Api
https://chart.googleapis.com//chart?chs="+ width+"x"+ width+"&cht=qr&chl=yourqrcodetext"
我刚刚添加了行
hints.put(EncodeHintType.ERROR\u CORRECTION,ErrorCorrectionLevel.H)对我的方法进行编码>以生成一个高校正级别,看一看,我可以看到生成的一个具有高校正级别。嗯,什么是BarcodeFormat
?这不在Android SDK中。你在用图书馆吗?@commonware我在用ZXing