1.二维码项目介绍
1.1Zxing
这个就不用多说了.
缺点:之前一个项目就是整合了没修改过的.
1.2.在EOE看到的,刚跑了一下,精简过,竖屏,够省事.所以就用这个开始整合进我的小项目吧.
补充1:刚又试了一下,如果要显示扫描后的图片话,这个还是不理想的.还是由于竖屏的问题.
补充2:看有网友评论说这个效率会低一些,说实话我是没仔细看,不知道到底影响有多大,其实如果太大,那就换回那个横屏的就是了.
1.3
2.二维码(QRCode)及一维码(条形码)的科普.
(以前只是为了整合应用,需求也只是能扫就行,对,结果我交差也是能用就行....
刚才在使用豆瓣购书单(Android版),发现它不识别二维码,商品的条形码也不识别.以前真的没注意这个问题,还觉得,ZXing挺牛逼,什么都识别挺好.
ISBN码是EAN_13的子集. 只是ISBN是以 978或979而已.
这个是ZXing
-
- public
static final BarcodeFormat QR_CODE = new BarcodeFormat("QR_CODE"); -
-
- public
static final BarcodeFormat DATA_MATRIX = new BarcodeFormat("DATA_MATRIX"); -
-
- public
static final BarcodeFormat UPC_E = new BarcodeFormat("UPC_E"); -
-
- public
static final BarcodeFormat UPC_A = new BarcodeFormat("UPC_A"); -
-
- public
static final BarcodeFormat EAN_8 = new BarcodeFormat("EAN_8"); -
-
- public
static final BarcodeFormat EAN_13 = new BarcodeFormat("EAN_13"); -
-
- public
static final BarcodeFormat UPC_EAN_EXTENSION = new BarcodeFormat("UPC_EAN_EXTENSION"); -
-
- public
static final BarcodeFormat CODE_128 = new BarcodeFormat("CODE_128"); -
-
- public
static final BarcodeFormat CODE_39 = new BarcodeFormat("CODE_39"); -
-
- public
static final BarcodeFormat CODE_93 = new BarcodeFormat("CODE_93"); -
-
- public
static final BarcodeFormat CODABAR = new BarcodeFormat("CODABAR"); -
-
- public
static final BarcodeFormat ITF = new BarcodeFormat("ITF"); -
-
- public
static final BarcodeFormat RSS14 = new BarcodeFormat("RSS14"); -
-
- public
static final BarcodeFormat PDF417 = new BarcodeFormat("PDF417"); -
-
- public
static final BarcodeFormat RSS_EXPANDED = new BarcodeFormat("RSS_EXPANDED");
public static final BarcodeFormat QR_CODE = new BarcodeFormat("QR_CODE");
public static final BarcodeFormat DATA_MATRIX = new BarcodeFormat("DATA_MATRIX");
public static final BarcodeFormat UPC_E = new BarcodeFormat("UPC_E");
public static final BarcodeFormat UPC_A = new BarcodeFormat("UPC_A");
public static final BarcodeFormat EAN_8 = new BarcodeFormat("EAN_8");
public static final BarcodeFormat EAN_13 = new BarcodeFormat("EAN_13");
public static final BarcodeFormat UPC_EAN_EXTENSION = new BarcodeFormat("UPC_EAN_EXTENSION");
public static final BarcodeFormat CODE_128 = new BarcodeFormat("CODE_128");
public static final BarcodeFormat CODE_39 = new BarcodeFormat("CODE_39");
public static final BarcodeFormat CODE_93 = new BarcodeFormat("CODE_93");
public static final BarcodeFormat CODABAR = new BarcodeFormat("CODABAR");
public static final BarcodeFormat ITF = new BarcodeFormat("ITF");
public static final BarcodeFormat RSS14 = new BarcodeFormat("RSS14");
public static final BarcodeFormat PDF417 = new BarcodeFormat("PDF417");
public static final BarcodeFormat RSS_EXPANDED = new BarcodeFormat("RSS_EXPANDED");
2.1 ISBN条形码.
新的国际标准书号在国际上简称“ISBN-13”。而前缀码979将作为ISBN-13备用的新系列号码
百科里看了半天,应该就是靠这句话识别条形码的区别的吧.
EAN-13 是要扫描的格式.
3.相关类(看到哪,记哪.能力有限,勿喷,哈哈)
3.1
扫描界面
3.2
扫描图案的类型. 二维码/一维码 就像豆瓣读书单一样,我只需要扫描EAN_13就好,不需要做的大而全.
3.3
解码过程的核心类.
3.4
根据二维码的Result解析相应的结果:ISBN/Tel/Email/Url 等 做人性化的话,就要多区分这些动作就是了.
4.实现方法
4.1 豆瓣书单的实现方式
4.2 一个小小的疑问.
-
if(ISBNResultParser.parse(rawResult) == null){ -
Message message = Message.obtain(activity.getHandler(), R.id.decode_failed); -
message.sendToTarget(); -
return ; - }
if(ISBNResultParser.parse(rawResult) == null){
Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
message.sendToTarget();
return ;
}
5.通过生成图片和解码图片
5.1 EnCode
-
public static Bitmap Encode(String str, BarcodeFormat barcodeFormat, int Width, -
int Height) throws WriterException { -
Hashtable new Hashtable -
// 指定纠错等级 - //
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); -
// 指定编码格式 -
hints.put(EncodeHintType.CHARACTER_SET, HTTP.UTF_8); -
// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败 -
BitMatrix matrix = new MultiFormatWriter().encode(str, barcodeFormat, -
Width, Height, hints); -
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] = 0xff000000; -
}else{ -
pixels[y * width + x] = 0xffffffff; -
} -
-
} -
} -
Bitmap bitmap = Bitmap.createBitmap(width, height, -
Bitmap.Config.ARGB_8888); -
// 通过像素数组生成bitmap,具体参考api -
bitmap.setPixels(pixels, 0, width, 0, 0, width, height); -
return bitmap; -
}
public static Bitmap Encode(String str, BarcodeFormat barcodeFormat, int Width,
int Height) throws WriterException {
Hashtablehints = new Hashtable(); // 指定纠错等级 // hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 指定编码格式 hints.put(EncodeHintType.CHARACTER_SET, HTTP.UTF_8); // 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败 BitMatrix matrix = new MultiFormatWriter().encode(str, barcodeFormat, Width, Height, hints); 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] = 0xff000000; }else{ pixels[y * width + x] = 0xffffffff; } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 通过像素数组生成bitmap,具体参考api bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }
5.2 DeCode
RGBLuminanceSource 这个类好像不在ZXing Android项目里.
- public
static Result DeCode(Bitmap bitmap){ -
Hashtable hints = new Hashtable(); -
hints.put(EncodeHintType.CHARACTER_SET, HTTP.UTF_8); -
-
RGBLuminanceSource source = new RGBLuminanceSource(bitmap); -
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); -
QRCodeReader reader2= new QRCodeReader(); -
Result result; -
try { -
result = reader2.decode(bitmap1,hints); -
return result ; -
} catch (NotFoundException e) { -
e.printStackTrace(); -
return null ; -
} catch (ChecksumException e) { -
e.printStackTrace(); -
return null ; -
} catch (FormatException e) { -
e.printStackTrace(); -
return null ; -
} - }
public static Result DeCode(Bitmap bitmap){
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, HTTP.UTF_8);
RGBLuminanceSource source = new RGBLuminanceSource(bitmap);
BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader2= new QRCodeReader();
Result result;
try {
result = reader2.decode(bitmap1,hints);
return result ;
} catch (NotFoundException e) {
e.printStackTrace();
return null ;
} catch (ChecksumException e) {
e.printStackTrace();
return null ;
} catch (FormatException e) {
e.printStackTrace();
return null ;
}
}
5.3 这里又出现了一个国人很悲剧的问题...
OK,二维码工作应该就这样结束了.
4660



被折叠的 条评论
为什么被折叠?



