人生就要挑战新难度——记zxing的深化
首先,我们来看看zxing一些基本介绍。
ZXing是一个开放源码的,用Java实现的多种格式的1D(注1d条码主要常见的条码)
/2D条码(主要是二维码)
图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。该项目可实现的条形码编码和解码。我们目前支持以下格式:
UPC-A,UPC-E EAN-8,EAN-13 代码128 创新及科技基金 RSS-14(所有的变体 RSS扩展(大多数变体) 数据矩阵 阿兹台克人(测试版质量) PDF 417(阿尔法的质量) Zxing库的主要部分支持以下几个功能:核心代码的使用、适用于J2SE客户端的版本、适用于Android客户端的版本(即BarcodeScanner)、Android的集成(通过Intent支持和BarcodeScanner的集成)等。 在android下生成二维码 在http://code.google.com/p/zxing/downloads/list下载zxing压缩包(我用的Zxing-1.7),解压后将core/src和javase/src中的com文件夹整体复制到你的java工程中,这两个包里面包含java所用的java源码。 相应的源代码如下:package com.easyoa.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Hashtable; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.BinaryBitmap; import com.google.zxing.DecodeHintType; import com.google.zxing.LuminanceSource; import com.google.zxing.MultiFormatReader; import com.google.zxing.MultiFormatWriter; import com.google.zxing.Reader; import com.google.zxing.ReaderException; import com.google.zxing.Result; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.ByteMatrix; import com.google.zxing.common.HybridBinarizer; public class Test { private static final int BLACK = 0xff000000; private static final int WHITE = 0xFFFFFFFF; * @param args public static void main(String[] args) { Test test=new Test(); test.encode(); test.decode(); //编码 * 在编码时需要将com.google.zxing.qrcode.encoder.Encoder.java中的 * static final String DEFAULT_BYTE_MODE_ENCODING = "ISO8859-1";修改为UTF-8,否则中文编译后解析不了 public void encode(){ try { String str = "姓名:曾驰文,性别:男,年龄:27,籍贯:湖南长沙,";// 二维码内容 String path = "D://test.png"; ByteMatrix byteMatrix; byteMatrix= new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 200, 200); File file = new File(path); writeToFile(byteMatrix, "png", file); } catch (Exception e) { e.printStackTrace(); public static void writeToFile(ByteMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); ImageIO.write(image, format, file); public static BufferedImage toBufferedImage(ByteMatrix matrix) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int x = 0; x width; x++) { for (int y = 0; y height; y++) { image.setRGB(x, y, matrix.get(x, y) == 0 ? BLACK:WHITE); return image; //解码 public void decode(){ try{ Reader reader = new MultiFormatReader(); String imgPath = "D://test.png"; File file = new File(imgPath); BufferedImage image; try { image = ImageIO.read(file); if (image == null) { System.out.println("Could not decode image"); LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; Hashtable hints= new Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); //解码设置编码方式为:utf-8, result = new MultiFormatReader().decode(bitmap,hints); String resultStr = result.getText(); System.out.println("解析后内容:"+resultStr); } catch (IOException ioe) { System.out.println(ioe.toString()); } catch (ReaderException re) { System.out.println(re.toString()); }catch(Exception ex){ System.out.println(ex.toString()); }
通过代码,我们可以得出下列的结论:
为了更好的生成相应的二维码,我们需要将相应的二维码内容转换成相应的流对象,将流对象转换成相应的图片,这图片是不同部分变成黑白的图片。
相应的解析的结果是:姓名:曾驰文,性别:男,年龄:27,籍贯:湖南长沙,
解析二维码
下面是 二维码从图片解析内容的分析与实现
解码的流程大致分成以下几个步骤:
1:获取摄像头byte[] data
2:对数据进行解析
在zxing客户端源码中
PreviewCallback 摄像头回调 data就是出自这里
PlanarYUVLuminanceSource 继承与LuminanceSource不同的数据原 YUV RGB
RGBLuminanceSource
AutoFocusCallback 自动对焦。不能自动对焦的手机zxing就不能发威了(这个处理相应的摄像头的过程中,在android系统下,由于是调用硬件设备,往往系统调度无法处理,从而实现后退键反映不及时的结果)
CameraManager 摄像头管理类。打开,关闭
DecodeThread 线程管理主要利用到了CountDownLatch
DecodeHandler 数据传输中枢。我理解DecodeThread控制线程,DecodeHandler发送数据
DecodeFormatManager 这个配置解码格式。一维码,二维码等
CaptureActivityHandler 这个是解码与avtivity中介。解码成功,失败都用她回调
ViewfinderView 我们看到的扫描框,搞花样就从她入手
同样,我们来看看源代码:
public class DecodeImageHandler { private static final String TAG = DecodeImageHandler.class.getSimpleName(); // 解码格式 private MultiFormatReader multiFormatReader; private static final String ISO88591 = "ISO8859_1"; // private Context mContext; public DecodeImageHandler(Context context) { // 解码的参数 Hashtable DecodeHintType, Object hints = new Hashtable DecodeHintType, Object // 能解析的编码类型 和 解析时使用的编码。 Vector BarcodeFormat decodeFormats = new Vector BarcodeFormat decodeFormats.addAll(DecodeFormatManager.ONE_D_FORMATS); decodeFormats.addAll(DecodeFormatManager.QR_CODE_FORMATS); decodeFormats.addAll(DecodeFormatManager.DATA_MATRIX_FORMATS); hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats); hints.put(DecodeHintType.CHARACTER_SET, ISO88591); init(context, hints); public DecodeImageHandler(Context context, Hashtable DecodeHintType, Object hints) { init(context, hints); private void init(Context context, Hashtable DecodeHintType, Object hints) { multiFormatReader = new MultiFormatReader(); multiFormatReader.setHints(hints); // mContext = context; public Result decode(Bitmap bitmap) { // 首先,要取得该图片的像素数组内容 int width = bitmap.getWidth(); int height = bitmap.getHeight(); //-------------------------------------------------- //rgb模式 int[] data = new int[width * height]; bitmap.getPixels(data, 0, width, 0, 0, width, height); Result rgbResult = rgbModeDecode(data, width, height); if (rgbResult != null) { data = null; return rgbResult; //---------------------------------------------------- //yuv byte[] bitmapPixels = new byte[width * height]; bitmap.getPixels(data, 0, width, 0, 0, width, height); // 将int数组转换为byte数组 for (int i = 0; i data.length; i++) { bitmapPixels[i] = (byte) data[i]; // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); Result yuvResult = yuvModeDecode(bitmapPixels, width, height); bitmapPixels = null; return yuvResult; // public Result decode(String path) throws IOException { // // 解析图片高和宽 // BitmapFactory.Options options = new BitmapFactory.Options(); // options.inJustDecodeBounds = true; // BitmapFactory.decodeFile(path, options); // //从图片直接获取byte[] // File file = new File(path); // FileInputStream is = new FileInputStream(file); // ByteArrayOutputStream os = new ByteArrayOutputStream(); // int len = -1; // byte[] buf = new byte[512]; // while ((len = is.read(buf)) != -1) { // os.write(buf, 0, len); // } // //关闭流 // try { // is.close(); // } finally { // if (is != null) { // is.close(); // } // } // //解析 // return decode(os.toByteArray(), options.outWidth, options.outHeight); // } public Result rgbModeDecode(int[] data, int width, int height) { Result rawResult = null; RGBLuminanceSource source = new RGBLuminanceSource(width, height, data); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = multiFormatReader.decodeWithState(bitmap); } catch (ReaderException re) { // continue } finally { multiFormatReader.reset(); //转换乱码 if (rawResult != null) { return converResult(rawResult); return rawResult; public Result yuvModeDecode(byte[] data, int width, int height) { Result rawResult = null; PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); try { rawResult = multiFormatReader.decodeWithState(bitmap); } catch (ReaderException re) { // continue } finally { multiFormatReader.reset(); //转换乱码 if (rawResult != null) { return converResult(rawResult); return rawResult; * 使用ISO88591进行解码,然后通过ISO88591在进行转换乱码 private Result converResult(Result rawResult) { //复制一个Result,并转码 String str = rawResult.getText(); String converText = null; try { converText = BarcodeUtils.converStr(str, ISO88591); } catch (UnsupportedEncodingException e) { Logger.getInstance(TAG).debug(e.toString()); // FIXME 转化失败--》1:结果置空 // --》2:把未解码的内容返回 if (converText != null) { return serResultText(rawResult, converText); } else { return rawResult; private Result serResultText(Result rawResult, String converText) { Result resultResult = new Result(converText, rawResult.getRawBytes(), rawResult.getResultPoints(), rawResult.getBarcodeFormat(), System.currentTimeMillis()); resultResult.putAllMetadata(rawResult.getResultMetadata()); return resultResult; }
我们可以看出:
①指定相应的系统的参数来解码byte数组中的内容。
②这样数组往往会出现乱码,我们需要经过crc等等的编码格式的校正。
③把相应的文字赋值给对话框。
这就是我对zxing的理解。
一文详解网易数帆数据生产力方法论 2021 年,网易数帆大数据团队正式提出数据生产力的理念,数据生产力从广义上讲,是指“通过使用数据,带来组织生产力的提升”;从狭义上讲,是指“数据采集、清洗、加工、可视化等数据处理和数据治理的软件生产能力以及持续运营能力”。
开发一对一直播平台源码,会面临这些技术难题 音视频压缩是为了减少音视频文件的体积,方便数据传输。音频数据和视频数据的原始格式不同,所以使用的编码格式不同,在选择编码格式时,一对一直播平台源码要选择编码效率和效果更好的编码,优化音视频文件传输和质量。
玉伯:前端的现状之痛及未来趋势 在 GMTC 北京上,我听了蚂蚁金服玉伯老师的分享。玉伯 2008 年加入阿里,在这期间,他和团队一起折腾过 KISSY、SeaJS、Ant Design、AntV 等开源项目,现在我们都喜欢用的语雀也是他团队的产品。
生产力再提速,618 互动项目进化之路 从2019年双十一的 “盖楼 ”到今年618的 “开列车”,在大促互动游戏背后,是业务多变性、产品稳定性和研发效率的多重博弈。本文介绍了淘系互动前端团队如何应对研发效率 & 产品稳定性的挑战,内容涵盖“互动智能测试” & “弹窗规模化生产”这两个技术方案。
3个因素看透 AI 技术架构方案的可行性 人工智能这几年发展的如火如荼,不仅在计算机视觉和自然语言处理领域发生了翻天覆地的变革,在其他领域也掀起了技术革新的浪潮。无论是在新业务上的尝试,还是对旧有业务对改造升级,AI 这个奔涌了 60 多年的“后浪”,正潜移默化的影响着我们传统的技术架构观念。
干货!3 个重要因素,带你看透 AI 技术架构方案的可行性 人工智能这几年发展的如火如荼,不仅在计算机视觉和自然语言处理领域发生了翻天覆地的变革,在其他领域也掀起了技术革新的浪潮。
相关文章
- 《征服 C 指针》笔记6:练习——挑战那些复杂的声明
- SQL挑战——如何高效生成编码
- 云原生时代下,容器安全的“四个挑战”和“两个关键”
- 给 K8s API “做减法”:阿里巴巴云原生应用管理的挑战和实践
- 【Python 22】52周存钱挑战2.0(列表list和math函数)
- 掌握元数据知识看这一篇就够了:从元数据概念、分类、作用、管理及目标、挑战到规划及体系设计等
- LCP 40. 心算挑战
- Python 爬取pythontip网站的挑战题目(附全部题目178题)
- 一起挑战pythontip的题目(16)
- 一起挑战pythontip的题目(5)
- 一起挑战pythontip的题目(11)
- 一起挑战pythontip的题目(19)
- 一起挑战pythontip的题目(7)
- Paper:《Explainable Artificial Intelligence (XAI): Concepts&可解释的人工智能:负责任人工智能的概念、分类法、机遇和挑战》翻译与解读02
- AI:2020年6月16日晚20点陆奇博士演讲《正视挑战把握创业创新机会》
- 挑战10的1,143,913次方种算法组合:这都不是事儿~~
- 《Kubernetes网络权威指南》读书笔记 | 天生不易:容器组网的挑战
- 给数据库带来的挑战
- 掌握元数据知识看这一篇就够了:从元数据概念、分类、作用、管理及目标、挑战到规划及体系设计等
- 【牛客网刷题系列 之 Verilog进阶挑战】~ 多bit MUX同步器