Initial commit
This commit is contained in:
211
lib/src/main/java/com/king/zxing/util/CodeUtils.java
Normal file
211
lib/src/main/java/com/king/zxing/util/CodeUtils.java
Normal file
@@ -0,0 +1,211 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Jenly Yu
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.king.zxing.util;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.EncodeHintType;
|
||||
import com.google.zxing.MultiFormatWriter;
|
||||
import com.google.zxing.WriterException;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.qrcode.QRCodeWriter;
|
||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
|
||||
*/
|
||||
public class CodeUtils {
|
||||
|
||||
private CodeUtils(){
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成二维码
|
||||
* @param content
|
||||
* @param heightPix
|
||||
* @return
|
||||
*/
|
||||
public static Bitmap createQRCode(String content, int heightPix) {
|
||||
return createQRCode(content,heightPix,null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成二维码
|
||||
* @param content
|
||||
* @param heightPix
|
||||
* @param logo
|
||||
* @return
|
||||
*/
|
||||
public static Bitmap createQRCode(String content, int heightPix, Bitmap logo) {
|
||||
//配置参数
|
||||
Map<EncodeHintType, Object> hints = new HashMap<>();
|
||||
hints.put( EncodeHintType.CHARACTER_SET, "utf-8");
|
||||
//容错级别
|
||||
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
|
||||
//设置空白边距的宽度
|
||||
hints.put(EncodeHintType.MARGIN, 1); //default is 4
|
||||
return createQRCode(content,heightPix,logo,hints);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成二维码
|
||||
* @param content
|
||||
* @param heightPix
|
||||
* @param logo
|
||||
* @param hints
|
||||
* @return
|
||||
*/
|
||||
public static Bitmap createQRCode(String content, int heightPix, Bitmap logo,Map<EncodeHintType,?> hints) {
|
||||
try {
|
||||
|
||||
// 图像数据转换,使用了矩阵转换
|
||||
BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, heightPix, heightPix, hints);
|
||||
int[] pixels = new int[heightPix * heightPix];
|
||||
// 下面这里按照二维码的算法,逐个生成二维码的图片,
|
||||
// 两个for循环是图片横列扫描的结果
|
||||
for (int y = 0; y < heightPix; y++) {
|
||||
for (int x = 0; x < heightPix; x++) {
|
||||
if (bitMatrix.get(x, y)) {
|
||||
pixels[y * heightPix + x] = 0xff000000;
|
||||
} else {
|
||||
pixels[y * heightPix + x] = 0xffffffff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 生成二维码图片的格式
|
||||
Bitmap bitmap = Bitmap.createBitmap(heightPix, heightPix, Bitmap.Config.ARGB_8888);
|
||||
bitmap.setPixels(pixels, 0, heightPix, 0, 0, heightPix, heightPix);
|
||||
|
||||
if (logo != null) {
|
||||
bitmap = addLogo(bitmap, logo);
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
} catch (WriterException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在二维码中间添加Logo图案
|
||||
*/
|
||||
private static Bitmap addLogo(Bitmap src, Bitmap logo) {
|
||||
if (src == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (logo == null) {
|
||||
return src;
|
||||
}
|
||||
|
||||
//获取图片的宽高
|
||||
int srcWidth = src.getWidth();
|
||||
int srcHeight = src.getHeight();
|
||||
int logoWidth = logo.getWidth();
|
||||
int logoHeight = logo.getHeight();
|
||||
|
||||
if (srcWidth == 0 || srcHeight == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (logoWidth == 0 || logoHeight == 0) {
|
||||
return src;
|
||||
}
|
||||
|
||||
//logo大小为二维码整体大小的1/6
|
||||
float scaleFactor = srcWidth * 1.0f / 6 / logoWidth;
|
||||
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
|
||||
try {
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawBitmap(src, 0, 0, null);
|
||||
canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);
|
||||
canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);
|
||||
|
||||
canvas.save(Canvas.ALL_SAVE_FLAG);
|
||||
canvas.restore();
|
||||
} catch (Exception e) {
|
||||
bitmap = null;
|
||||
e.getStackTrace();
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成条形码
|
||||
* @param content
|
||||
* @param format
|
||||
* @param desiredWidth
|
||||
* @param desiredHeight
|
||||
* @return
|
||||
*/
|
||||
public static Bitmap createBarCode(String content,BarcodeFormat format, int desiredWidth, int desiredHeight) {
|
||||
return createBarCode(content,format,desiredWidth,desiredHeight,null);
|
||||
|
||||
}
|
||||
/**
|
||||
* 生成条形码
|
||||
* @param content
|
||||
* @param format
|
||||
* @param desiredWidth
|
||||
* @param desiredHeight
|
||||
* @param hints
|
||||
* @return
|
||||
*/
|
||||
public static Bitmap createBarCode(String content,BarcodeFormat format, int desiredWidth, int desiredHeight,Map<EncodeHintType,?> hints) {
|
||||
if(TextUtils.isEmpty(content)){
|
||||
return null;
|
||||
}
|
||||
final int WHITE = 0xFFFFFFFF;
|
||||
final int BLACK = 0xFF000000;
|
||||
|
||||
MultiFormatWriter writer = new MultiFormatWriter();
|
||||
try {
|
||||
BitMatrix result = writer.encode(content, format, desiredWidth,
|
||||
desiredHeight, hints);
|
||||
int width = result.getWidth();
|
||||
int height = result.getHeight();
|
||||
int[] pixels = new int[width * height];
|
||||
// All are 0, or black, by default
|
||||
for (int y = 0; y < height; y++) {
|
||||
int offset = y * width;
|
||||
for (int x = 0; x < width; x++) {
|
||||
pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user