1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { 17 BarcodeFormat, 18 MultiFormatReader, 19 RGBLuminanceSource, 20 BinaryBitmap, 21 HybridBinarizer 22} from '@ohos/zxing'; 23import image from '@ohos.multimedia.image'; 24 25export interface ImageWH{ 26 width: number, 27 height: number 28}; 29export default class QRCode { 30 async decode(image: image.PixelMap, params: ImageWH): Promise<string> { 31 const width: number = params.width; 32 const height: number = params.height; 33 let num = image.getPixelBytesNumber(); 34 let arrayBuffer: ArrayBuffer = new ArrayBuffer(num); 35 await image.readPixelsToBuffer(arrayBuffer); 36 const int32Array = new Int32Array(arrayBuffer); 37 const luminanceSource = new RGBLuminanceSource(int32Array, width, height); 38 console.log("yyc luminanceSource") 39 const binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource)); 40 console.log("yyc binaryBitmap") 41 const reader = new MultiFormatReader(); 42 console.log("yyc reader") 43 let result = reader.decode(binaryBitmap); 44 console.log("yyc result") 45 let text = result.getText(); 46 return text; 47 } 48}