zxing白边java_ZXing生成无白边条形码

参考连接中的代码作了简化,StrToBrCode.class直接用

package de.safetytaxfree.tuishuibaoshangmiprinter.Printer;

import android.graphics.Bitmap;

import android.text.TextUtils;

import com.google.zxing.BarcodeFormat;

import com.google.zxing.EncodeHintType;

import com.google.zxing.MultiFormatWriter;

import com.google.zxing.common.BitMatrix;

import com.google.zxing.oned.Code128Writer;

import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.util.HashMap;

import java.util.Map;

public class StrToBrCode {

/**

@param expectWidth 期望的宽度

@param maxWidth 最大允许宽度

* @param contents 生成条形码的内容

* @param height

* @return

*/

public static Bitmap getBarCodeWithoutPadding(int expectWidth,int maxWidth,int height,String contents){

int realWidth = getBarCodeNoPaddingWidth(expectWidth,contents,maxWidth);

return syncEncodeBarcode(contents, realWidth, height,0);

}

private static int getBarCodeNoPaddingWidth(int expectWidth,String contents,int maxWidth){

boolean[] code = new Code128Writer(). encode(contents);

int inputWidth = code.length;

//code:210000000000000082 code.length:134 expectWidth:397 maxWidth:435

// Add quiet zone on both sides.

//int fullWidth = inputWidth + 0;

double outputWidth = (double) Math.max(expectWidth, inputWidth);

double multiple = outputWidth / inputWidth;

//multiple:2.962686567164179

//优先取大的

int returnVal =0;

int ceil = (int) Math.ceil(multiple);

if(inputWidth * ceil <= maxWidth){

returnVal = inputWidth * ceil;

}else {

int floor = (int) Math.floor(multiple);

returnVal = inputWidth * floor;

}

return returnVal;

}

/**

* 同步创建条形码图片

*

* @param content 要生成条形码包含的内容

* @param width 条形码的宽度,单位px

* @param height 条形码的高度,单位px

* @param textSize 字体大小,单位px,如果等于0则不在底部绘制文字

* @return 返回生成条形的位图

*

* 白边问题:

* https://blog.csdn.net/sunshinwong/article/details/50156017

*已知高度,计算宽度:

*

*/

private static Bitmap syncEncodeBarcode(String content, int width, int height, int textSize) {

if (TextUtils.isEmpty(content)) {

return null;

}

Map hints = new HashMap<>();

hints.put(EncodeHintType.CHARACTER_SET, "utf-8");

hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

hints.put(EncodeHintType.MARGIN, 0);

try {

BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, width, height, hints);

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] = 0xff000000;

} else {

pixels[y * width + x] = 0xffffffff;

}

}

}

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

bitmap.setPixels(pixels, 0, width, 0, 0, width, height);

if (textSize > 0) {

}

return bitmap;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值