生成条形码/二维码支持自定义颜色 (#73)

This commit is contained in:
jenly1314
2019-12-27 14:49:14 +08:00
parent f1d16dceae
commit 72378b0a6d

View File

@@ -32,7 +32,6 @@ import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
@@ -68,6 +67,17 @@ public final class CodeUtils {
return createQRCode(content,heightPix,null);
}
/**
* 生成二维码
* @param content 二维码的内容
* @param heightPix 二维码的高
* @param codeColor 二维码的颜色
* @return
*/
public static Bitmap createQRCode(String content, int heightPix,int codeColor) {
return createQRCode(content,heightPix,null,codeColor);
}
/**
* 生成我二维码
* @param content 二维码的内容
@@ -76,7 +86,19 @@ public final class CodeUtils {
* @return
*/
public static Bitmap createQRCode(String content, int heightPix, Bitmap logo) {
return createQRCode(content,heightPix,logo,0.2f);
return createQRCode(content,heightPix,logo,Color.BLACK);
}
/**
* 生成我二维码
* @param content 二维码的内容
* @param heightPix 二维码的高
* @param logo logo大小默认占二维码的20%
* @param codeColor 二维码的颜色
* @return
*/
public static Bitmap createQRCode(String content, int heightPix, Bitmap logo,int codeColor) {
return createQRCode(content,heightPix,logo,0.2f,codeColor);
}
/**
@@ -104,10 +126,35 @@ public final class CodeUtils {
* @param heightPix 二维码的高
* @param logo 二维码中间的logo
* @param ratio logo所占比例 因为二维码的最大容错率为30%所以建议ratio的范围小于0.3
* @param hints
* @param codeColor 二维码的颜色
* @return
*/
public static Bitmap createQRCode(String content, int heightPix, Bitmap logo,@FloatRange(from = 0.0f,to = 1.0f)float ratio,int codeColor) {
//配置参数
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,ratio,hints,codeColor);
}
public static Bitmap createQRCode(String content, int heightPix, Bitmap logo,@FloatRange(from = 0.0f,to = 1.0f)float ratio,Map<EncodeHintType,?> hints) {
return createQRCode(content,heightPix,logo,ratio,hints,Color.BLACK);
}
/**
* 生成二维码
* @param content 二维码的内容
* @param heightPix 二维码的高
* @param logo 二维码中间的logo
* @param ratio logo所占比例 因为二维码的最大容错率为30%所以建议ratio的范围小于0.3
* @param hints
* @param codeColor 二维码的颜色
* @return
*/
public static Bitmap createQRCode(String content, int heightPix, Bitmap logo,@FloatRange(from = 0.0f,to = 1.0f)float ratio,Map<EncodeHintType,?> hints,int codeColor) {
try {
// 图像数据转换,使用了矩阵转换
@@ -118,9 +165,9 @@ public final class CodeUtils {
for (int y = 0; y < heightPix; y++) {
for (int x = 0; x < heightPix; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * heightPix + x] = 0xff000000;
pixels[y * heightPix + x] = codeColor;
} else {
pixels[y * heightPix + x] = 0xffffffff;
pixels[y * heightPix + x] = Color.WHITE;
}
}
}
@@ -207,35 +254,75 @@ public final class CodeUtils {
* @return
*/
public static String parseQRCode(String bitmapPath, Map<DecodeHintType,?> hints){
try {
QRCodeReader reader = new QRCodeReader();
Result result = null;
RGBLuminanceSource source = getRGBLuminanceSource(compressBitmap(bitmapPath));
if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
result = reader.decode(bitmap,hints);
} catch (Exception e) {
//解析失败则通过GlobalHistogramBinarizer 再试一次
BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source));
try {
result = reader.decode(bitmap1);
} catch (NotFoundException ne) {
}
} finally {
reader.reset();
}
}
Result result = parseQRCodeResult(bitmapPath,hints);
if(result != null){
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解析二维码图片
* @param bitmapPath
* @param hints
* @return
*/
public static Result parseQRCodeResult(String bitmapPath, Map<DecodeHintType,?> hints){
Result result = null;
try{
QRCodeReader reader = new QRCodeReader();
RGBLuminanceSource source = getRGBLuminanceSource(compressBitmap(bitmapPath));
if (source != null) {
boolean isReDecode;
try {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
result = reader.decode(bitmap,hints);
isReDecode = false;
} catch (Exception e) {
isReDecode = true;
}
if(isReDecode){
try {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source.invert()));
result = reader.decode(bitmap,hints);
isReDecode = false;
} catch (Exception e) {
isReDecode = true;
}
}
if(isReDecode){
try{
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
result = reader.decode(bitmap,hints);
isReDecode = false;
}catch (Exception e){
isReDecode = true;
}
}
if(isReDecode && source.isRotateSupported()){
try{
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source.rotateCounterClockwise()));
result = reader.decode(bitmap,hints);
}catch (Exception e){
}
}
reader.reset();
}
}catch (Exception e){
e.printStackTrace();
}
return result;
}
/**
* 解析一维码/二维码图片
* @param bitmapPath
@@ -263,34 +350,76 @@ public final class CodeUtils {
* @return
*/
public static String parseCode(String bitmapPath, Map<DecodeHintType,Object> hints){
try {
MultiFormatReader reader = new MultiFormatReader();
reader.setHints(hints);
Result result = null;
RGBLuminanceSource source = getRGBLuminanceSource(compressBitmap(bitmapPath));
if (source != null) {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
result = reader.decodeWithState(bitmap);
} catch (Exception e) {//解析失败时则通过GlobalHistogramBinarizer 再试一次
BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source));
try {
result = reader.decodeWithState(bitmap1);
} catch (Exception ne) {
}
} finally {
reader.reset();
}
}
Result result = parseCodeResult(bitmapPath,hints);
if(result != null){
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解析一维码/二维码图片
* @param bitmapPath
* @param hints 解析编码类型
* @return
*/
public static Result parseCodeResult(String bitmapPath, Map<DecodeHintType,Object> hints){
Result result = null;
try{
MultiFormatReader reader = new MultiFormatReader();
reader.setHints(hints);
RGBLuminanceSource source = getRGBLuminanceSource(compressBitmap(bitmapPath));
if (source != null) {
boolean isReDecode;
try {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
result = reader.decodeWithState(bitmap);
isReDecode = false;
} catch (Exception e) {
isReDecode = true;
}
if(isReDecode){
try {
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source.invert()));
result = reader.decodeWithState(bitmap);
isReDecode = false;
} catch (Exception e) {
isReDecode = true;
}
}
if(isReDecode){
try{
BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
result = reader.decodeWithState(bitmap);
isReDecode = false;
}catch (Exception e){
isReDecode = true;
}
}
if(isReDecode && source.isRotateSupported()){
try{
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source.rotateCounterClockwise()));
result = reader.decodeWithState(bitmap);
}catch (Exception e){
}
}
reader.reset();
}
}catch (Exception e){
e.printStackTrace();
}
return result;
}
/**
@@ -338,6 +467,17 @@ public final class CodeUtils {
}
/**
* 生成条形码
* @param content
* @param desiredWidth
* @param desiredHeight
* @return
*/
public static Bitmap createBarCode(String content, int desiredWidth, int desiredHeight) {
return createBarCode(content,BarcodeFormat.CODE_128,desiredWidth,desiredHeight,null);
}
/**
* 生成条形码
* @param content
@@ -348,7 +488,23 @@ public final class CodeUtils {
*/
public static Bitmap createBarCode(String content,BarcodeFormat format, int desiredWidth, int desiredHeight) {
return createBarCode(content,format,desiredWidth,desiredHeight,null);
}
public static Bitmap createBarCode(String content, int desiredWidth, int desiredHeight, boolean isShowText) {
return createBarCode(content,BarcodeFormat.CODE_128,desiredWidth,desiredHeight,null,isShowText,40,Color.BLACK);
}
/**
* 生成条形码
* @param content
* @param desiredWidth
* @param desiredHeight
* @param isShowText
* @param codeColor
* @return
*/
public static Bitmap createBarCode(String content, int desiredWidth, int desiredHeight, boolean isShowText,@ColorInt int codeColor) {
return createBarCode(content,BarcodeFormat.CODE_128,desiredWidth,desiredHeight,null,isShowText,40,codeColor);
}
/**
@@ -378,6 +534,34 @@ public final class CodeUtils {
return createBarCode(content,format,desiredWidth,desiredHeight,hints,isShowText,40,Color.BLACK);
}
/**
* 生成条形码
* @param content
* @param format
* @param desiredWidth
* @param desiredHeight
* @param isShowText
* @param codeColor
* @return
*/
public static Bitmap createBarCode(String content, BarcodeFormat format, int desiredWidth, int desiredHeight, boolean isShowText,@ColorInt int codeColor) {
return createBarCode(content,format,desiredWidth,desiredHeight,null,isShowText,40,codeColor);
}
/**
* 生成条形码
* @param content
* @param format
* @param desiredWidth
* @param desiredHeight
* @param hints
* @param isShowText
* @return
*/
public static Bitmap createBarCode(String content, BarcodeFormat format, int desiredWidth, int desiredHeight, Map<EncodeHintType,?> hints, boolean isShowText,@ColorInt int codeColor) {
return createBarCode(content,format,desiredWidth,desiredHeight,hints,isShowText,40,codeColor);
}
/**
* 生成条形码
* @param content
@@ -387,15 +571,15 @@ public final class CodeUtils {
* @param hints
* @param isShowText
* @param textSize
* @param textColor
* @param codeColor
* @return
*/
public static Bitmap createBarCode(String content,BarcodeFormat format, int desiredWidth, int desiredHeight,Map<EncodeHintType,?> hints,boolean isShowText,int textSize,@ColorInt int textColor) {
public static Bitmap createBarCode(String content,BarcodeFormat format, int desiredWidth, int desiredHeight,Map<EncodeHintType,?> hints,boolean isShowText,int textSize,@ColorInt int codeColor) {
if(TextUtils.isEmpty(content)){
return null;
}
final int WHITE = 0xFFFFFFFF;
final int BLACK = 0xFF000000;
final int WHITE = Color.WHITE;
final int BLACK = codeColor;
MultiFormatWriter writer = new MultiFormatWriter();
try {
@@ -416,7 +600,7 @@ public final class CodeUtils {
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
if(isShowText){
return addCode(bitmap,content,textSize,textColor,textSize/2);
return addCode(bitmap,content,textSize,codeColor,textSize/2);
}
return bitmap;
} catch (WriterException e) {