同步操作将从 OpenHarmony-TPC/zxing 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
zxing是一个解析/生成一维码/二维码的库。
1D product | 1D industrial | 2D |
---|---|---|
UPC-A | Code 39 | QR Code |
UPC-E | Code 93 | Data Matrix |
EAN-8 | Code 128 | Aztec |
EAN-13 | Codabar | PDF 417 |
RSS-14 | ITF | MaxiCode |
RSS-Expanded |
ohpm install @ohos/zxing
OpenHarmony ohpm 环境配置等更多内容,请参考如何安装 OpenHarmony ohpm 包
import {MultiFormatReader, BarcodeFormat, DecodeHintType, RGBLuminanceSource, BinaryBitmap, HybridBinarizer } from "@ohos/zxing";
const hints = new Map();
const formats = [BarcodeFormat.QR_CODE];
hints.set(DecodeHintType.POSSIBLE_FORMATS, formats);
const reader = new MultiFormatReader();
reader.setHints(hints);
const luminanceSource = new RGBLuminanceSource(luminances, width, height);
const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
let result = reader.decode(binaryBitmap);
let text = result.getText();
import {BarcodeFormat, MultiFormatWriter, BitMatrix, ZXingStringEncoding, EncodeHintType} from '@ohos/zxing';
const encodeHintTypeMap = new Map();
//设置二维码边空白的宽度
encodeHintTypeMap.set(EncodeHintType.MARGIN, 0);
const writer: MultiFormatWriter = new MultiFormatWriter();
let matrix: BitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, encodeHintTypeMap);
OpenHarmony上我们是用image组件显示图片的,所以需要将matrix转化成pixelMap这样才可以显示在image组件上面。
1.需要将matrix转成pixelMap的buffer。
2.在根据这个buffer去创建pixelMap。
3.输入的解析生成码的一些限制:
codabar只能是数字
ena8 只能是7位数字
ena13只能是12位数字
ITF码只能是数字,且需要双数,长度要>=6才能解析生成码
upcA只能数字,且长度只能是11位。
upcE只能数字,且长度只能是7位
具体操作细节可以看demo代码,主要转换逻辑都封装在imageUtils工具类中。
类名 | 方法名 | 功能 |
---|---|---|
QRCodeWriter | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成QRCode码。 |
DataMatrixWriter | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成DataMatrix码。 |
AztecWriter | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成Aztec码。 |
PDF417Writer | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成PDF417码。 |
Code39Writer | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成Code39码。 |
Code93Writer | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成Code93码。 |
Code128Writer | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成Code128码。 |
CodaBarWriter | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成CodaBar码。 |
ITFWriter | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成ITF码。 |
UPCAWriter | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成UPCA码。 |
UPCEWriter | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成UPCE码。 |
EAN8Writer | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成EAN8码。 |
EAN13Writer | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 生成EAN13码。 |
MultiFormatWriter | encode(contents: string,format: BarcodeFormat,width: int,height:int,hints: Map<EncodeHintType, any>): BitMatrix | 这是一个工厂类方法,它为请求的条形码/二维码格式找到适当的编写器子类,并使用提供的内容编码二维码/条形码。 |
类名 | 方法名 | 功能 |
---|---|---|
QRCodeReader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析QRCode码。 |
DataMatrixReader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析DataMatrix码。 |
AztecReader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析Aztec码。 |
PDF417Reader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析PDF417码。 |
MaxiCodeReader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析MaxiCode码。 |
Code39Reader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析Code39码。 |
Code93Reader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析Code93码。 |
CodaBarReader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析CodaBar码。 |
Code128Reader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析Code128码。 |
ITFReader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析ITF码。 |
UPCAReader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析UPCA码。 |
UPCEReader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析UPCE码。 |
EAN8Reader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析EAN8码。 |
EAN13Reader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析EAN13码。 |
RSS14Reader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析RSS14码。 |
RSSExpandedReader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 解析RSSExpanded码。 |
MultiFormatReader | decode(image: BinaryBitmap, hints?: Map<DecodeHintType, any> | null): Result; | 这是一个工厂类方法,它为请求的条形码/二维码格式找到适当的解码器子类,并使用提供的内容解码二维码/条形码。 |
组件名名 | ||
---|---|---|
CameraView |
类名 | 方法名 | 功能 |
---|---|---|
init(context:Context) | CameraService.getInstance().init(getContext()) | 打开相机并初始化 |
destroy | CameraService.getInstance().destroy() | 关闭相机 |
release | CameraService.getInstance().release() | 释放 |
类名 | 方法名 | 功能 |
---|---|---|
setObject | GlobalContext.getContext().setObject("key",value) | 设置值 |
getObject | GlobalContext.getContext().getObject("key") | 获取值 |
在下述版本验证通过: DevEco Studio: 4.1 Canary(4.1.3.322), SDK: API11 (4.1.0.36)
DevEco Studio: 4.1 Canary(4.1.3.220), SDK: API11 (4.1.2.1)
DevEco Studio: 4.0 Beta2(4.0.3.500), SDK: API10 (4.0.10.7)
DevEco Studio: 4.0 Canary2(4.0.3.317), SDK: API10 (4.0.9.5)
DevEco Studio: 3.1 Beta2(3.1.0.400), SDK: API9 Release(3.2.11.9)
|---- Zxing
| |---- entry # 示例代码文件夹
| |---- library # zxing库文件夹
| |---- index.ets # 对外接口
| |---- README.MD # 安装使用方法
使用过程中发现任何问题都可以提 Issue 给我们,当然,我们也非常欢迎你给我们发 PR 。
本项目基于 Apache License 2.0 ,请自由的享受和参与开源。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。