[LayaNative 2.0]Native(android)下创建二维码并显示在页面上
Laya2.3.1,Native2.6.1亲测有效.
Android创建二维码用到zxing库 ,在app的依赖项(dependencies)里添加: implementation 'com.google.zxing:core:3.3.0'
// ts脚本,点击按钮创建二维码并显示
private onBtnClickShare() {
let base64OfQrCode:string = androidplat.createQrCode("https://www.layabox.com/", 180, 180);
if (base64OfQrCode.length>0) {
let img:Laya.Image = new Laya.Image();
img.skin = "data:image/png;base64,"+base64OfQrCode;
img.pos(100, 300);
Laya.stage.addChild(img);
}
}
// androidplat,创建二维码
createQrCode(url:string, width:number, height:number):string {
return this._bridge.call("createQrCode", url, width, height);
}
// 以下android代码
// JSBridge.java
Android创建二维码用到zxing库 ,在app的依赖项(dependencies)里添加: implementation 'com.google.zxing:core:3.3.0'
// ts脚本,点击按钮创建二维码并显示
private onBtnClickShare() {
let base64OfQrCode:string = androidplat.createQrCode("https://www.layabox.com/", 180, 180);
if (base64OfQrCode.length>0) {
let img:Laya.Image = new Laya.Image();
img.skin = "data:image/png;base64,"+base64OfQrCode;
img.pos(100, 300);
Laya.stage.addChild(img);
}
}
// androidplat,创建二维码
createQrCode(url:string, width:number, height:number):string {
return this._bridge.call("createQrCode", url, width, height);
}
// 以下android代码
// JSBridge.java
public static String createQrCode(final String content, final double width, final double height) {
Bitmap bitmap = createQRCodeBitmap(content, (int)width, (int)height, "UTF-8", "H", "1", Color.BLACK, Color.WHITE);
if (null != bitmap) {
String base64OfBitmap = bitmapToBase64(bitmap);
if (null != base64OfBitmap) {
base64OfBitmap.replace("\n", "");
return base64OfBitmap;
}
}
return "";
}
@Nullable
public static Bitmap createQRCodeBitmap(String content, int width, int height,
@Nullable String character_set, @Nullable String error_correction, @Nullable String margin,
@ColorInt int color_black, @ColorInt int color_white){
/** 1.参数合法性判断 */
if(TextUtils.isEmpty(content)){ // 字符串内容判空
return null;
}
if(width < 0 || height < 0){ // 宽和高都需要>=0
return null;
}
try {
/** 2.设置二维码相关配置,生成BitMatrix(位矩阵)对象 */
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
if(!TextUtils.isEmpty(character_set)) {
hints.put(EncodeHintType.CHARACTER_SET, character_set); // 字符转码格式设置
}
if(!TextUtils.isEmpty(error_correction)){
hints.put(EncodeHintType.ERROR_CORRECTION, error_correction); // 容错级别设置
}
if(!TextUtils.isEmpty(margin)){
hints.put(EncodeHintType.MARGIN, margin); // 空白边距设置
}
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
/** 3.创建像素数组,并根据BitMatrix(位矩阵)对象为数组元素赋颜色值 */
int pixels = new int[width * height];
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
if(bitMatrix.get(x, y)){
pixels[y * width + x] = color_black; // 黑色色块像素设置
} else {
pixels[y * width + x] = color_white; // 白色色块像素设置
}
}
}
/** 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,之后返回Bitmap对象 */
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
/**
* bitmap转为base64
* @param bitmap
* @return
*/
public static String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.flush();
baos.close();
byte bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
没有找到相关结果
已邀请:
要回复问题请先登录
0 个回复