您的位置:首页 > 编程语言 > Go语言

条形码、二维码以及二维码添加logo

2016-03-09 17:06 1101 查看
/**
* 用字符串生成二维码

* @param str
* @author zhouzhe@lenovo-cw.com
* @return
* @throws WriterException
*/
public Bitmap Create2DCode(String str, int picWidth, int picHeight) {
try {
// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
Hashtable hints = new Hashtable();
// 设置容错率。调整二维码线条粗细
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix matrix = new MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, picWidth, picHeight, hints);
int width = matrix.getWidth();
int height = matrix.getHeight();
// 二维矩阵转为一维像素数组,也就是一直横着排了
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = 0xff000000;
} else {
pixels[y * width + x] = 0xffffffff;
}
}
}

Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
// 通过像素数组生成bitmap,具体参考api
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
Bitmap logoBit = BitmapFactory.decodeResource(getResources(),
R.drawable.logo_qr);
Bitmap result = addLogo(bitmap, logoBit);
return result;
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}

/**
* 生成条形码

* @param context
* @param contents
*            需要生成的内容
* @param desiredWidth
*            生成条形码的宽度
* @param desiredHeight
*            生成条形码的高度
* @param displayCode
*            是否在条形码下方显示内容
* @return
*/
public Bitmap createBarcode(Context context, String contents,
int desiredWidth, int desiredHeight, boolean displayCode) {
Bitmap resultBitmap = null;
/**
* 图片两端所保留的空白的宽度
*/
/**
* 条形码的编码类型
*/
BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128;

if (displayCode) {
// 条形码
Bitmap barcodeBitmap = encodeAsBitmap(contents, barcodeFormat,
desiredWidth, desiredHeight);
// 条形码下方数字图片
Bitmap codeBitmap = creatCodeBitmap(contents, desiredWidth,
DisplayUtil.dip2px(20), context);
// 将上面图片合并
resultBitmap = mixtureBitmap(barcodeBitmap, codeBitmap);
} else {
resultBitmap = encodeAsBitmap(contents, barcodeFormat,
desiredWidth, desiredHeight);
}

return resultBitmap;
}

/**
* 生成条形码的Bitmap

* @param contents
*            需要生成的内容
* @param format
*            编码格式
* @param desiredWidth
* @param desiredHeight
* @return
* @throws WriterException
*/
protected Bitmap encodeAsBitmap(String contents, BarcodeFormat format,
int desiredWidth, int desiredHeight) {
final int WHITE = 0xFFFFFFFF;
final int BLACK = 0xFF000000;

MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix result = null;
try {
result = writer.encode(contents, format, desiredWidth,
desiredHeight, null);
} catch (WriterException e) {
e.printStackTrace();
}

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;
}

/**
* 生成显示编码的Bitmap(条形码下的数字)

* @param contents
* @param width
* @param height
* @param context
* @return
*/
protected Bitmap creatCodeBitmap(String contents, int width, int height,
Context context) {
TextView tv = new TextView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(layoutParams);
contents = contents.substring(4);
tv.setTextSize(16);
String input = Utils.returnFormatString(contents, 4);
tv.setText(input);
// tv.setHeight(height);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setWidth(width);
tv.setDrawingCacheEnabled(true);
tv.setTextColor(getResources().getColor(R.color.venuelist_title));
tv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());

tv.buildDrawingCache();
Bitmap bitmapCode = tv.getDrawingCache();
return bitmapCode;
}

/**
* 将两个Bitmap合并成一个

* @param first
* @param second
* @return
*/
protected Bitmap mixtureBitmap(Bitmap first, Bitmap second) {
if (first == null || second == null) {
return null;
}

int marginW = 10;
Bitmap newBitmap = Bitmap
.createBitmap(
first.getWidth(),
first.getHeight() + second.getHeight()
+ DisplayUtil.dip2px(10), Config.ARGB_8888);
Canvas cv = new Canvas(newBitmap);
cv.drawBitmap(first, marginW, 0, null);
cv.drawBitmap(second, 0, first.getHeight() + DisplayUtil.dip2px(10),
null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();

return newBitmap;
}

/**
*  * 在二维码中间添加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;
}
// float scaleFactor = 0.8f;
// 以下-30是因为在生成二维码时周围都有一定的空白区,-30相当于网上平移30个像素
Bitmap bitmap = Bitmap.createBitmap(srcWidth,
srcHeight - DisplayUtil.dip2px(15), Bitmap.Config.ARGB_8888);
try {
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(src, 0, -DisplayUtil.dip2px(15), null);
// canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight /
// 2);
canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2,
(srcHeight - logoHeight) / 2 - DisplayUtil.dip2px(15), null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
} catch (Exception e) {
bitmap = null;
e.getStackTrace();
}
return bitmap;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: