Scan Kit有提供“识别本地图片”的功能;此功能需要接入方应用调用detectBarcode.decode方法并传入待识别图片的uri即可;<p id="p52232263333">接口名</p> <p id="p19223102603318">描述</p> <p id="p1422332663320">decode(inputImage: InputImage, options?: scanBarcode.ScanOptions): Promise<Array<scanBarcode.ScanResult>></p> <p id="p1722312611336">启动图片识码,通过InputImage传入图片信息,通过ScanOptions进行识码参数设置(options为可选参数),使用Promise异步回调返回识码结果。</p> <p id="p9223226153317">decode(inputImage: InputImage, options: scanBarcode.ScanOptions, callback: AsyncCallback<Array<scanBarcode.ScanResult>>): void</p> <p id="p19223192603310">启动图片识码,通过InputImage传入图片信息,通过ScanOptions进行识码参数设置,使用Callback异步回调返回识码结果。</p> <p id="p122231526113310">decode(inputImage: InputImage, callback: AsyncCallback<Array<scanBarcode.ScanResult>>): void</p> <p id="p6223172683315">启动图片识码,通过InputImage传入图片信息,使用Callback异步回调返回识码结果。</p> 基于第一点,实现此功能可以分为三个步骤,分别是:a. 应用可以先将图片下载到本地并保存到沙箱;b. 获取到保存到沙箱的图片uri;c. 使用“识别本地图片”能力识别图片二维码即可;参考代码如下:import { scanCore, scanBarcode, detectBarcode } from '@kit.ScanKit'; // 导入功能涉及的权限申请、回调接口 import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { image } from '@kit.ImageKit'; import { http } from '@kit.NetworkKit'; import { common } from '@kit.AbilityKit'; import { fileIo as fs, fileUri } from '@kit.CoreFileKit'; @Entry @Component struct ScanBarCodePage { @State pixelMap: image.PixelMap | string | undefined = undefined @State scanResult: string = '' loadImageWithUrl(url: string) { try { // 使用request下载图片并在回调函数中保存图片到相册 http.createHttp().request(url, { method: http.RequestMethod.GET, connectTimeout: 60000, readTimeout: 60000 }, (error: BusinessError, data: http.HttpResponse) => { this.loadImageCallback(error, data); }) } catch (e) { console.log(`testTAG ====== e:${e}`); } } //下载图片的回调函数 async loadImageCallback(error: BusinessError, data: http.HttpResponse) { if (http.ResponseCode.OK === data.responseCode) { let imageBuffer: ArrayBuffer = data.result as ArrayBuffer; this.saveImage(imageBuffer) } else { console.error("testTAG==error occurred when image downloaded!") } } //保存图片至沙箱 async saveImage(imageBuffer: ArrayBuffer) { // 获取应用文件路径 let context = getContext(this) as common.UIAbilityContext; let filesDir = context.filesDir; // 新建并打开文件 let file = fs.openSync(filesDir + '/testimage.jpg', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); // 写入一段内容至文件 fs.writeSync(file.fd, imageBuffer); //获取已存在沙箱的图片uri const imageUri = fileUri.getUriFromPath(filesDir + '/testimage.jpg') this.pixelMap = imageUri console.log(`testTAG: imageUri` + imageUri); // 关闭文件 fs.closeSync(file); let options: scanBarcode.ScanOptions = { scanTypes: [scanCore.ScanType.ALL], enableMultiMode: true, } //进行码图识别 let inputImage: detectBarcode.InputImage = { uri: imageUri } // 调用图片识码接口 detectBarcode.decode(inputImage, options).then((result: Array<scanBarcode.ScanResult>) => { hilog.info(0x0001, 'testTAG', `Succeeded in getting ScanResult by promise with options, result is ${JSON.stringify(result)}`); this.scanResult = JSON.stringify(result) }).catch((error: BusinessError) => { hilog.error(0x0001, 'testTAG', `Failed to get ScanResult by promise with options. Code: ${error.code}, message: ${error.message}`); }); } build() { Column() { Button('识别网络图像数据').onClick(() => { this.loadImageWithUrl("https://example.com") }) Text(this.scanResult) // 获取生成码后显示 if (this.pixelMap) { Image(this.pixelMap).width(300).height(300).objectFit(ImageFit.Contain) } } .backgroundColor('#DDDDDD') .width('100%') .height('100%') } }
a. 应用可以先将图片下载到本地并保存到沙箱;
b. 获取到保存到沙箱的图片uri;
c. 使用“识别本地图片”能力识别图片二维码即可;
参考代码如下: