2.定义ConnectionCallback回调,实现连接能力引擎成功与否后的操作。
ConnectionCallback connectionCallback = new ConnectionCallback() {
@Override
public void onServiceConnect() {
// Do something when service connects successfully
}
@Override
public void onServiceDisconnect() {
// Do something when service connects unsuccessfully
}
};
3.调用VisionManager.init()方法,将此工程的context和connectionCallback 作为入参,建立与能力引擎的连接,context应为ohos.aafwk.ability.Ability或ohos.aafwk.ability.AbilitySlice的实例或子类实例。
int result = VisionManager.init(context, connectionCallback);
5.定义码生成图像的尺寸,并根据图像大小分配字节流数组空间。
final int SAMPLE_LENGTH = 152;
byte[] byteArray = new byte[SAMPLE_LENGTH * SAMPLE_LENGTH * 4];
6.调用IBarcodeDetector的detect()方法,根据输入的字符串信息生成相应的二维码图片字节流。
int result = barcodeDetector.detect("This is a TestCase of IBarcodeDetector", byteArray, SAMPLE_LENGTH, SAMPLE_LENGTH);
如果返回值为0,表明调用成功。后续可以利用API将解码流转换为图片源,简要示例如下:
InputStream inputStream = new ByteArrayInputStream(byteArray);
ImageSource imageSource = ImageSource.create(inputStream, null);
7.当码生成能力使用完毕后,调用IBarcodeDetector的release()方法,释放资源。
result = barcodeDetector.release();