联博以太坊(326681.com)_Android动态天生二维码
发表时间:2020-12-27 浏览量:9
原创
Android动态天生二维码
什么是二维码
二维码又称二维条码,常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动装备上超盛行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能示意更多的数据类型。
动态天生二维码方案
在查找二维码天生方案时,发现许多方案的源头都指向了GitHub的开源库https://github.com/zxing/zxing。
1. ZXing简介:
ZXing全称zebra crossing,翻译过来就是『斑马线』的意思。ZXing是一个接纳Java实现的、开源的、支持多花样(一维/二维)的条形码图像处置库。
其中,QRCode花样就是我们常说的二维码花样。
注:QRCode(Quick Response Code:快速响应码)是二维条形码中最常用的一种花样,以是许多人直接将QRCode翻译为二维码,而且连百度百科都这样称谓,笔者也暂时就这么称谓了。
2. ZXing库引入
对于开发者来讲,我们需要下载ZXing库的一个jar包(core-x.x.x.jar)或者通过添加依赖的方式引入库文件,详细方式如下:
方式一:ZXing提供了Maven库,让我们可以凭据自己的需要选择想要的jar包版本举行下载。Maven库:https://repo1.maven.org/maven2/com/google/zxing/core/
,,www.u-healer.com采用以太坊区块链高度哈希值作为统计数据,联博以太坊统计数据开源、公平、无任何作弊可能性。联博统计免费提供API接口,支持多语言接入。
方式二(推荐):对于使用AndroidStudio开发的程序员而言,可能更习惯于在.gradle文件中添加依赖。详细代码如下(3.3.0是笔者使用时的最新版本,想知道最新版本是多少可以去Maven库查):
dependencies {
compile 'com.google.zxing:core:3.3.0'
}
3. ZXing库的封装
/**
* @ClassName: QRCodeUtil
* @Description: 二维码工具类
* @Author Wangnan
* @Date 2017/2/10
*/
public class QRCodeUtil {
/**
* 建立二维码位图
*
* @param content 字符串内容
* @param size 位图宽&高(单元:px)
* @return
*/
@Nullable
public static Bitmap createQRCodeBitmap(@Nullable String content, int size){
return createQRCodeBitmap(content, size, "UTF-8", "H", "4", Color.BLACK, Color.WHITE, null, null, 0F);
}
/**
* 建立二维码位图 (自定义黑、白色块颜色)
*
* @param content 字符串内容
* @param size 位图宽&高(单元:px)
* @param color_black 玄色色块的自定义颜色值
* @param color_white 白色色块的自定义颜色值
* @return
*/
@Nullable
public static Bitmap createQRCodeBitmap(@Nullable String content, int size, @ColorInt int color_black, @ColorInt int color_white){
return createQRCodeBitmap(content, size, "UTF-8", "H", "4", color_black, color_white, null, null, 0F);
}
/**
* 建立二维码位图 (带Logo小图片)
*
* @param content 字符串内容
* @param size 位图宽&高(单元:px)
* @param logoBitmap logo图片
* @param logoPercent logo小图片在二维码图片中的占比巨细,局限[0F,1F]。超出局限->默认使用0.2F
* @return
*/
@Nullable
public static Bitmap createQRCodeBitmap(String content, int size, @Nullable Bitmap logoBitmap, float logoPercent){
return createQRCodeBitmap(content, size, "UTF-8", "H", "4", Color.BLACK, Color.WHITE, null, logoBitmap, logoPercent);
}
/**
* 建立二维码位图 (Bitmap颜色取代玄色) 注重!!!注重!!!注重!!! 选用的Bitmap图片一定不能有白色色块,否则会识别不出来!!!
*
* @param content 字符串内容
* @param size 位图宽&高(单元:px)
* @param targetBitmap 目的图片 (若是targetBitmap != null, 玄色色块将会被该图片像素色值替换)
* @return
*/
@Nullable
public static Bitmap createQRCodeBitmap(String content, int size, Bitmap targetBitmap){
return createQRCodeBitmap(content, size, "UTF-8", "H", "4", Color.BLACK, Color.WHITE, targetBitmap, null, 0F);
}
/**
* 建立二维码位图 (支持自定义设置和自定义样式)
*
* @param content 字符串内容
* @param size 位图宽&高(单元:px)
* @param character_set 字符集/字符转码花样 (支持花样:{@link CharacterSetECI })。传null时,zxing源码默认使用 "ISO-8859-1"
* @param error_correction 容错级别 (支持级别:{@link ErrorCorrectionLevel })。传null时,zxing源码默认使用 "L"
* @param margin 空白边距 (可修改,要求:整型且>=0), 传null时,zxing源码默认使用"4"。
* @param color_black 玄色色块的自定义颜色值
* @param color_white 白色色块的自定义颜色值
* @param targetBitmap 目的图片 (若是targetBitmap != null, 玄色色块将会被该图片像素色值替换)
* @param logoBitmap logo小图片
* @param logoPercent logo小图片在二维码图片中的占比巨细,局限[0F,1F],超出局限->默认使用0.2F。
* @return
*/
@Nullable
public static Bitmap createQRCodeBitmap(@Nullable String content, int size,
@Nullable String character_set, @Nullable String error_correction, @Nullable String margin,
@ColorInt int color_black, @ColorInt int color_white, @Nullable Bitmap targetBitmap,
@Nullable Bitmap logoBitmap, float logoPercent){
/** 1.参数合法性判断 */
if(TextUtils.isEmpty(content)){ // 字符串内容判空
return null;
}
if(size <= 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, size, size, hints);
/** 3.凭据BitMatrix(位矩阵)工具为数组元素赋颜色值 */
if(targetBitmap != null){
targetBitmap = Bitmap.createScaledBitmap(targetBitmap, size, size, false);
}
int[] pixels = new int[size * size];
for(int y = 0; y < size; y++){
for(int x = 0; x < size; x++){
if(bitMatrix.get(x, y)){ // 玄色色块像素设置
if(targetBitmap != null) {
pixels[y * size + x] = targetBitmap.getPixel(x, y);
} else {
pixels[y * size + x] = color_black;
}
} else { // 白色色块像素设置
pixels[y * size + x] = color_white;
}
}
}
/** 4.建立Bitmap工具,凭据像素数组设置Bitmap每个像素点的颜色值,之后返回Bitmap工具 */
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
/** 5.为二维码添加logo小图标 */
if(logoBitmap != null){
return addLogo(bitmap, logoBitmap, logoPercent);
}
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
/**
* 向一张图片中心添加logo小图片(图片合成)
*
* @param srcBitmap 原图片
* @param logoBitmap logo图片
* @param logoPercent 百分比 (用于调整logo图片在原图片中的显示巨细, 取值局限[0,1], 传值不合法时使用0.2F)
* 原图片是二维码时,建议使用0.2F,百分比过大可能导致二维码扫描失败。
* @return
*/
@Nullable
private static Bitmap addLogo(@Nullable Bitmap srcBitmap, @Nullable Bitmap logoBitmap, float logoPercent){
/** 1. 参数合法性判断 */
if(srcBitmap == null){
return null;
}
if(logoBitmap == null){
return srcBitmap;
}
if(logoPercent < 0F || logoPercent > 1F){
logoPercent = 0.2F;
}
/** 2. 获取原图片和Logo图片各自的宽、高值 */
int srcWidth = srcBitmap.getWidth();
int srcHeight = srcBitmap.getHeight();
int logoWidth = logoBitmap.getWidth();
int logoHeight = logoBitmap.getHeight();
/** 3. 盘算画布缩放的宽高比 */
float scaleWidth = srcWidth * logoPercent / logoWidth;
float scaleHeight = srcHeight * logoPercent / logoHeight;
/** 4. 使用Canvas绘制,合成图片 */
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(srcBitmap, 0, 0, null);
canvas.scale(scaleWidth, scaleHeight, srcWidth/2, srcHeight/2);
canvas.drawBitmap(logoBitmap, srcWidth/2 - logoWidth/2, srcHeight/2 - logoHeight/2, null);
return bitmap;
}
}
4.动态天生二维码
Bitmap bitmap = QRCodeUtils.createQRCodeBitmap(qrUrl, 500);
5. 效果
©著作权归作者所有:来自51CTO博客作者mb5fe55be0b9ac7的原创作品,如需转载,请注明出处,否则将追究法律责任
java
0
珍藏
上一篇:Android Studio 把... 下一篇:Android开发规范