本模块支持通过文本或字节数组生成码图。目前已支持的码制式为EAN-8、EAN-13、UPC-A、UPC-E、Codabar、Code 39、Code 93、Code 128、ITF-14、QR Code、Data Matrix、PDF417、Aztec。暂时不支持MULTIFUNCTIONAL CODE生成。
起始版本: 4.1.0(11)
导入模块
import { generateBarcode } from '@kit.ScanKit';
ErrorCorrectionLevel
纠错率枚举。
元服务API: 从版本5.0.2(14)开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Multimedia.Scan.GenerateBarcode
起始版本 : 4.1.0(11)
名称 | 值 | 说明 |
---|---|---|
LEVEL_L | 0 | 7%纠错率。 |
LEVEL_M | 1 | 15%纠错率。 |
LEVEL_Q | 2 | 25%纠错率。 |
LEVEL_H | 3 | 30%纠错率。 |
CreateOptions
生成码参数。
元服务API: 从版本5.0.2(14)开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Multimedia.Scan.GenerateBarcode
起始版本: 4.1.0(11)
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
scanType | scanCore.[ScanType] | 否 | 否 | 码类型。 |
width | number | 否 | 否 | 码图宽,单位:px。取值范围:[200, 4096]。 |
height | number | 否 | 否 | 码图高,单位:px。取值范围:[200, 4096]。 |
margin | number | 否 | 是 | 边距,单位:px,默认值为1,取值范围:[1, 10]。 |
level | [ErrorCorrectionLevel] | 否 | 是 | 纠错水平,默认值为LEVEL_H。注意此参数只在生成QR码时有效。 |
backgroundColor | number | 否 | 是 | 生成码图背景颜色,HEX格式颜色,默认为白色(0xffffff)。 |
pixelMapColor | number | 否 | 是 | 生成码图颜色,HEX格式颜色,默认为黑色(0x000000)。 |
说明
生成码参数建议:
-
码图颜色和背景
建议使用默认颜色和背景:黑色码图、白色背景。如果码图颜色和背景对比度较小会影响识别率。
-
码图边距
建议使用默认边距1,单位:px,取值范围:[1, 10]。
-
码图大小
- 生成QR Code、Data Matrix、Aztec类型的码图时,输入的width和height值相同且均大于等于200小于等于4096,单位:px,否则生成的码图过小会影响识别。
- 生成EAN-8、EAN-13、UPC-A、UPC-E、Codabar、Code 39、Code 93、Code 128、ITF-14、PDF417类型的码图时,建议输入的width和height值比例为2:1,并且width值需大于400,单位:px,否则生成的码图会过小影响识别。
示例:
import { scanCore, generateBarcode } from '@kit.ScanKit';
// 以QR码为例
let options: generateBarcode.CreateOptions = {
scanType: scanCore.ScanType.QR_CODE,
height: 200,
width: 200,
backgroundColor: 0xFFFFFF,
pixelMapColor: 0x000000,
margin: 1,
level: generateBarcode.ErrorCorrectionLevel.LEVEL_H
}
generateBarcode.createBarcode
createBarcode(content: string, options: CreateOptions): Promise<image.PixelMap>
通过文本生成码图,使用Promise异步回调返回生成的码图。
元服务API: 从版本5.0.2(14)开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Multimedia.Scan.GenerateBarcode
起始版本 : 4.1.0(11)
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
content | string | 是 | 码内容字符串 |
options | [CreateOptions] | 是 | 用于设置生成码图的参数。 |
返回值:
类型 | 说明 |
---|---|
Promise<image.[PixelMap]> | Promise对象,返回生成的码图对象。 |
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed. |
1000500001 | Internal error. |
示例:
import { image } from '@kit.ImageKit';
import { scanCore, generateBarcode } from '@kit.ScanKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
// 以QR码为例,码图生成参数
let content: string = 'Huawei@HMSCore';
let options: generateBarcode.CreateOptions = {
scanType: scanCore.ScanType.QR_CODE,
height: 200,
width: 200
}
try {
generateBarcode.createBarcode(content, options).then((result: image.PixelMap) => {
// 码图生成接口,成功返回PixelMap格式图片
hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting PixelMap by promise with options`);
}).catch((error: BusinessError) => {
hilog.error(0x0001, '[Scan Sample]',
`Failed to get PixelMap by promise with options. Code: ${error.code}, message: ${error.message}`);
});
} catch (error) {
hilog.error(0x0001, '[Scan Sample]', `Failed to generateBarcode. Code: ${error.code}, message: ${error.message}`);
}
content参数限制条件:
生成码类型 | 参数建议内容 |
---|---|
QR Code | 支持中文,不超过512字符长度,如果内容过长会导致码复杂,影响识别。 |
Aztec | 支持中文,不超过512字符长度,如果内容过长会导致码复杂,影响识别。 |
PDF417 | 支持中文,不超过512字符长度,如果内容过长会导致码复杂,影响识别。 |
Data Matrix | 不超过512字符长度,如果内容过长会导致码复杂,影响识别。 |
UPC-A | 支持11位数字输入,只支持数字,生成包含12位数字的码图,包含最后一位校验数字。 |
UPC-E | 支持7位数字输入,只支持数字,首位需要是0或1,生成包含8位数字的码图,包含最后一位校验数字。 |
ITF-14 | 支持80位以内数字输入,并且需要是偶数位,只支持数字,生成包含偶数位数字的码图,如果内容过长会导致码复杂,影响识别。 |
EAN-8 | 支持7位数字输入,只支持数字,生成包含8位数字的码图,包含最后一位校验数字。 |
EAN-13 | 支持12位数字输入,只支持数字,首位不可以是0,生成包含13位数字的码图,包含最后一位校验数字 |
Code 39 | 不超过80字节长度,字符集可以是数字、大小写字母和- . $ / + % * SPACE英文格式符号(请注意:一个小写字母占用2个字节)。 |
Code 93 | 不超过80字节长度,字符集可以是数字、大小写字母和- . $ / + % * SPACE英文格式符号(请注意:一个小写字母占用2个字节)。 |
Code 128 | 不超过80字节长度,字符集可以是数字、大小写字母和- . $ / + % * SPACE英文格式符号(请注意:一个小写字母占用1个字节)。 |
Codabar | 不超过512字符长度,起始/终止符可以是ABCD中的任一个(特殊情况下,TN*E也会编码成ABCD,推荐使用ABCD)。其他字符可以是数字和- . $ / : +英文格式符号。 |
generateBarcode.createBarcode
createBarcode(content: string, options: CreateOptions, callback: AsyncCallback<image.PixelMap>): void
通过文本生成码图,使用Callback异步回调返回生成的码图。
元服务API: 从版本5.0.2(14)开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Multimedia.Scan.GenerateBarcode
起始版本: 4.1.0(11)
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
content | string | 是 | 码内容字符串。 |
options | [CreateOptions] | 是 | 用于设置生成码图的参数。 |
callback | AsyncCallback<image.[PixelMap]> | 是 | 回调函数。当码图生成成功,err为undefined,data为生成的码图对象image.PixelMap;否则为错误对象。 |
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed. |
1000500001 | Internal error. |
示例:
import { image } from '@kit.ImageKit';
import { scanCore, generateBarcode } from '@kit.ScanKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
// 以QR码为例,码图生成参数
let content: string = 'Huawei@HMSCore';
let options: generateBarcode.CreateOptions = {
scanType: scanCore.ScanType.QR_CODE,
height: 200,
width: 200
}
try {
generateBarcode.createBarcode(content, options, (error: BusinessError, result: image.PixelMap) => {
if (error) {
hilog.error(0x0001, '[Scan Sample]',
`Failed to get PixelMap by callback with options. Code: ${error.code}, message: ${error.message}`);
return;
}
// 码图生成接口,成功返回PixelMap格式图片
hilog.info(0x0001, '[Scan Sample]', `Succeeded in getting PixelMap by callback with options`);
});
} catch (error) {
hilog.error(0x0001, '[Scan Sample]', `Failed to generateBarcode. Code: ${error.code}, message: ${error.message}`);
}
generateBarcode.createBarcode
createBarcode(content: ArrayBuffer, options: CreateOptions): Promise<image.PixelMap>
通过字节数组生成码图,使用Promise异步回调返回生成的码图。
元服务API: 从版本5.0.2(14)开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Multimedia.Scan.GenerateBarcode
起始版本 : 5.0.0(12)
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
content | ArrayBuffer | 是 | 码内容字节数组。 |
options | [CreateOptions] | 是 | 用于设置生成码图的参数,ScanType仅支持QR Code。 |
返回值:
类型 | 说明 |
---|---|
Promise<image.[PixelMap]> | Promise对象,返回生成的码图对象。 |
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed. |
1000500001 | Internal error. |
示例:
import { image } from '@kit.ImageKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { scanCore, generateBarcode } from '@kit.ScanKit';
import { buffer } from '@kit.ArkTS';
const TAG: string = 'Create barcode';
@Entry
@Component
struct Index {
@State pixelMap: image.PixelMap | undefined = undefined
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Button('generateBarcode Promise').onClick(() => {
this.pixelMap = undefined;
let content: string =
'0177C10DD10F7768600202312110000063458FD14112345678FFFFD381012610b746365409210201b66636540ad0200020000000000110e617003201000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006645fbec664358ECF657CB40693c92da';
let contentBuffer: ArrayBuffer = buffer.from(content, 'hex').buffer; // 通过包含十六进制字符的字符串创建Buffer
let options: generateBarcode.CreateOptions = {
scanType: scanCore.ScanType.QR_CODE,
height: 400,
width: 400
}
try {
// 码图生成接口,成功返回PixelMap格式图片
generateBarcode.createBarcode(contentBuffer, options).then((pixelMap: image.PixelMap) => {
this.pixelMap = pixelMap;
hilog.info(0x0001, TAG, 'Succeeded in creating barCode');
}).catch((error: BusinessError) => {
hilog.error(0x0001, TAG, `Failed to create barCode. Code: ${error.code}, message: ${error.message}`);
});
} catch (error) {
hilog.error(0x0001, TAG, `Failed to generateBarcode. Code: ${error.code}, message: ${error.message}`);
}
})
// 获取生成码后显示
if (this.pixelMap) {
Image(this.pixelMap).width(300).height(300).objectFit(ImageFit.Contain)
}
}
.width('100%')
.height('100%')
}
}
content参数限制条件:
纠错水平 | 参数内容限制 |
---|---|
LEVEL_L | 字节数组长度限制建议不超过2048。 |
LEVEL_M | 字节数组长度限制建议不超过2048。 |
LEVEL_Q | 字节数组长度限制建议不超过1536。 |
LEVEL_H | 字节数组长度限制建议不超过1024。 |
最后呢
很多开发朋友不知道需要学习那些鸿蒙技术?鸿蒙开发岗位需要掌握那些核心技术点?为此鸿蒙的开发学习必须要系统性的进行。
而网上有关鸿蒙的开发资料非常的少,假如你想学好鸿蒙的应用开发与系统底层开发。你可以参考这份资料,少走很多弯路,节省没必要的麻烦。由两位前阿里高级研发工程师联合打造的《鸿蒙NEXT星河版OpenHarmony开发文档》里面内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(Harmony NEXT)技术知识点
如果你是一名Android、Java、前端等等开发人员,想要转入鸿蒙方向发展。可以直接领取这份资料辅助你的学习。下面是鸿蒙开发的学习路线图。
针对鸿蒙成长路线打造的鸿蒙学习文档。话不多说,我们直接看详细鸿蒙(OpenHarmony )手册(共计1236页)与鸿蒙(OpenHarmony )开发入门视频,帮助大家在技术的道路上更进一步。
- 《鸿蒙 (OpenHarmony)开发学习视频》
- 《鸿蒙生态应用开发V2.0白皮书》
- 《鸿蒙 (OpenHarmony)开发基础到实战手册》
- OpenHarmony北向、南向开发环境搭建
- 《鸿蒙开发基础》
- 《鸿蒙开发进阶》
- 《鸿蒙开发实战》
总结
鸿蒙—作为国家主力推送的国产操作系统。部分的高校已经取消了安卓课程,从而开设鸿蒙课程;企业纷纷跟进启动了鸿蒙研发。
并且鸿蒙是完全具备无与伦比的机遇和潜力的;预计到年底将有 5,000 款的应用完成原生鸿蒙开发,未来将会支持 50 万款的应用。那么这么多的应用需要开发,也就意味着需要有更多的鸿蒙人才。鸿蒙开发工程师也将会迎来爆发式的增长,学习鸿蒙势在必行! 自↓↓↓拿