📄 barcode128.java
字号:
// starts with 2 digits ..
nextToken = START_C;
out.append(nextToken);
if (ucc)
{
out.append(FNC1);
}
out.append(getPackedRawDigits(text, index, 2));
index = 2;
}
else if (character < ' ')
{
// the character is a ascii control character (0x00 - 0x1f) ..
nextToken = START_A;
out.append(nextToken);
if (ucc)
{
out.append(FNC1);
}
out.append((char) (character + 64));
index = 1;
}
else
{
out.append(nextToken);
if (ucc)
{
out.append(FNC1);
}
out.append((char) (character - ' '));
index = 1;
}
while (index < tLen)
{
switch (nextToken)
{
case START_A:
{
if (isNextCharsDigits(text, index, 4))
{
nextToken = START_C;
out.append(CODE_AB_TO_C);
out.append(getPackedRawDigits(text, index, 4));
index += 4;
}
else
{
character = text.charAt(index);
index += 1;
if (character > '_')
{
nextToken = START_B;
out.append(CODE_AC_TO_B);
out.append((char) (character - ' '));
}
else if (character < ' ')
{
out.append((char) (character + 64));
}
else
{
out.append((char) (character - ' '));
}
}
}
break;
case START_B:
{
if (isNextCharsDigits(text, index, 4))
{
nextToken = START_C;
out.append(CODE_AB_TO_C);
out.append(getPackedRawDigits(text, index, 4));
index += 4;
}
else
{
character = text.charAt(index++);
if (character < ' ')
{
nextToken = START_A;
out.append(CODE_BC_TO_A);
out.append((char) (character + 64));
}
else
{
out.append((char) (character - ' '));
}
}
}
break;
case START_C:
{
if (isNextCharsDigits(text, index, 2))
{
out.append(getPackedRawDigits(text, index, 2));
index += 2;
}
else
{
character = text.charAt(index++);
if (character < ' ')
{
nextToken = START_A;
out.append(CODE_BC_TO_A);
out.append((char) (character + 64));
}
else
{
nextToken = START_B;
out.append(CODE_AC_TO_B);
out.append((char) (character - ' '));
}
}
}
break;
}
}
return out.toString();
}
/**
* Generates the bars. The input has the actual barcodes, not
* the human readable text.
* @param text the barcode
* @return the bars
*/
public static byte[] getBarsCode128Raw(final String text)
{
final StringBuffer b = new StringBuffer();
final int idx = text.indexOf('\uffff');
if (idx >= 0)
{
b.append(text.substring(0, idx));
}
else
{
b.append(text);
}
// Calculate the checksum
int chkSum = b.charAt(0);
for (int k = 1; k < b.length(); ++k)
{
chkSum += k * b.charAt(k);
}
chkSum = chkSum % 103;
b.append((char) chkSum);
final byte[] bars = new byte[(b.length() + 1) * 6 + 7];
int k;
for (k = 0; k < b.length(); ++k)
{
System.arraycopy(BARS[b.charAt(k)], 0, bars, k * 6, 6);
}
System.arraycopy(BARS_STOP, 0, bars, k * 6, 7);
return bars;
}
public String getStrippedCode()
{
return getCode();
}
public String getRawText()
{
return getRawText(getCode(), isUccCode());
}
/** Gets the maximum area that the barcode and the text, if
* any, will occupy. The lower left corner is always (0, 0).
* @return the size the barcode occupies.
*/
public Dimension2D getBarcodeSize()
{
float fontX = 0;
float fontY = 0;
final FontDefinition font = getFont();
if (font != null)
{
final float baseline = getBaseline();
if (baseline > 0)
{
fontY = baseline - getFontDescent(font.getFont());
}
else
{
fontY = -baseline + font.getFontSize();
}
final String fullCode = getStrippedCode();
final BarcodeSizeCalculator calc = new BarcodeSizeCalculator(font);
fontX = calc.getStringWidth(fullCode, 0, fullCode.length());
}
final float fullWidth = Math.max(getFullWidth(), fontX);
final float fullHeight = getBarHeight() + fontY;
return new FloatDimension(fullWidth, fullHeight);
}
private float getFullWidth()
{
final String rawText = getRawText();
final int len = rawText.length();
final float fullWidth = (len + 2) * 11 * getMinWidth() + 2 * getMinWidth();
return fullWidth;
}
/** Creates an <CODE>Image</CODE> with the barcode.
* @param barColor the color of the bars. It can be <CODE>null</CODE>
* @param textColor the color of the text. It can be <CODE>null</CODE>
* @return the <CODE>Image</CODE>
*/
public Image createImageWithBarcode(final Color barColor, final Color textColor)
{
if (barColor == null)
{
throw new NullPointerException("BarColor must not be null");
}
if (textColor == null)
{
throw new NullPointerException("TextColor must not be null");
}
final String fullCode = getStrippedCode();
final String bCode = getRawText();
// int len = bCode.length();
final float fullWidth = getFullWidth();
float barStartX = 0;
float barStartY = 0;
float textStartX = 0;
float textStartY = 0;
float textWidth = 0;
final FontDefinition font = getFont();
if (font != null)
{
final BarcodeSizeCalculator calc = new BarcodeSizeCalculator(font);
textWidth = calc.getStringWidth(fullCode, 0, fullCode.length());
final float baseline = getBaseline();
if (baseline > 0)
{
textStartY = baseline - getFontDescent(font.getFont());
}
else
{
textStartY = -baseline + font.getFontSize();
barStartY = textStartY + getBaseline();
}
}
if (getTextAlignment() == ElementAlignment.RIGHT)
{
if (textWidth > fullWidth)
{
barStartX = textWidth - fullWidth;
}
else
{
textStartX = fullWidth - textWidth;
}
}
else if (getTextAlignment() == ElementAlignment.CENTER)
{
if (textWidth > fullWidth)
{
barStartX = (textWidth - fullWidth) / 2;
}
else
{
textStartX = (fullWidth - textWidth) / 2;
}
}
final int imageX = (int) Math.max (fullWidth, textWidth);
final int imageY = (int) (getBarHeight() + textStartY);
final BufferedImage image = new BufferedImage (imageX, imageY, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2 = image.createGraphics();
final byte[] bars = getBarsCode128Raw(bCode);
g2.setPaint(barColor);
for (int k = 0; k < bars.length; ++k)
{
final float w = bars[k] * getMinWidth();
// print every other bar ..
if ((k % 2) == 0)
{
final Rectangle2D.Float rect = new Rectangle2D.Float(barStartX, barStartY, w, getBarHeight());
g2.fill(rect);
}
barStartX += w;
}
if (font != null)
{
g2.setFont(font.getFont());
g2.setPaint(textColor);
g2.drawString(fullCode, textStartX, textStartY);
}
g2.dispose();
return image;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -