中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
码生成能够根据开发者给定的字符串信息和二维码图片尺寸,返回相应的二维码图片字节流。调用方可以通过二维码字节流生成二维码图片
官网文档地址:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ai-code-genration-guidelines-0000001050822133
目前已知问题:中文弄得二维码扫码出来是乱码!如何解决谁知道?
附代码:
/** * https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ai-code-genration-guidelines-0000001050822133 * <p> * 约束与限制 * 当前仅支持生成QR二维码(Quick Response Code)。由于QR二维码算法的限制,字符串信息的长度不能超过2953个字符。 * 生成的二维码图片的宽度不能超过1920像素,高度不能超过1680像素。由于QR二维码是通过正方形阵列承载信息的, * 建议二维码图片采用正方形,当二维码图片采用长方形时,会在QR二维码信息的周边区域留白。 * * @since 2021-04-21 */ public class QRCodeAbilitySlice extends BaseAbilitySlice { final int SAMPLE_LENGTH = 152 * 3; byte[] byteArray = new byte[SAMPLE_LENGTH * SAMPLE_LENGTH * 4]; IBarcodeDetector barcodeDetector; Image codeImage; TextField codeTextField; @Override protected void onStart(Intent intent) { super.onStart(intent); setUIContent(ResourceTable.Layout_slice_qr_code); codeImage = findCastComponentById(ResourceTable.Id_code_image); codeTextField = findCastComponentById(ResourceTable.Id_code_text_field); //调用VisionManager.init()方法 int initResult = VisionManager.init(QRCodeAbilitySlice.this, connectionCallback); log("initResult: %d", initResult); findCastComponentById(ResourceTable.Id_create_code_btn) .setClickedListener(component -> { if (barcodeDetector != null) { // 6 调用IBarcodeDetector的detect()方法,根据输入的字符串信息生成相应的二维码图片字节流 //如果返回值为0,表明调用成功。 String text = codeTextField.getText(); if (text.isEmpty()) { return; } int detectResult = barcodeDetector.detect(text, byteArray, SAMPLE_LENGTH, SAMPLE_LENGTH); if (detectResult == 0) { ImageSource imageSourceNoOptions = ImageSource.create(byteArray, null); PixelMap pixelMapNoOptions = imageSourceNoOptions.createPixelmap(null); codeImage.setPixelMap(pixelMapNoOptions); } log("detectResult: %d", detectResult); log("byteArray: %s", LogStringUtil.array2String(byteArray)); } }); } @Override protected void onBackground() { super.onBackground(); // 7 ,当码生成能力使用完毕后,调用IBarcodeDetector的release()方法,释放资源。 int barcodeDetectorReleaseResult = barcodeDetector.release(); log("barcodeDetectorReleaseResult: %d", barcodeDetectorReleaseResult); //8,调用VisionManager.destroy()方法,断开与能力引擎的连接。 VisionManager.destroy(); } // 2 定义ConnectionCallback回调,实现连接能力引擎成功与否后的操作。 ConnectionCallback connectionCallback = new ConnectionCallback() { @Override public void onServiceConnect() { log("Vision onServiceConnect"); // 4 实例化IBarcodeDetector接口 barcodeDetector = VisionManager.getBarcodeDetector(QRCodeAbilitySlice.this); log("barcodeDetector: %s", barcodeDetector); } @Override public void onServiceDisconnect() { log("Vision onServiceDisconnect"); } }; }
微信扫码分享