Java 使用条形码4J创建gif条形码
我正在尝试使用Java 使用条形码4J创建gif条形码,java,servlets,barcode,gif,Java,Servlets,Barcode,Gif,我正在尝试使用barcode4jlib创建条形码。这就是我得到的:它看起来很光滑。我就是这样做的: BitmapCanvasProvider provider = null; Interleaved2Of5Bean bean = new Interleaved2Of5Bean(); int dpi = 100; // Configure the barcode generator bean.setModuleW
barcode4j
lib创建条形码。这就是我得到的:它看起来很光滑。我就是这样做的:
BitmapCanvasProvider provider = null;
Interleaved2Of5Bean bean = new Interleaved2Of5Bean();
int dpi = 100;
// Configure the barcode generator
bean.setModuleWidth(UnitConv.in2mm(1.0f /
dpi)); // makes the narrow
// bar
// width exactly
// one
// pixel
bean.doQuietZone(false);
provider =
new BitmapCanvasProvider(100, BufferedImage.TYPE_BYTE_GRAY,
true, 0);
bean.generateBarcode(provider, request.getParameter("barcode"));
provider.finish();
BufferedImage barcodeImage = provider.getBufferedImage();
response.setContentType("image/gif");
OutputStream outputStream = response.getOutputStream();
ImageIO.write(barcodeImage, "gif", outputStream);
outputStream.close();
如何增加它的定义?好的,我找到了一个解决方案。我就是这样做的:
Interleaved2Of5Bean bean = new Interleaved2Of5Bean();
bean.setHeight(10d);
bean.doQuietZone(false);
OutputStream out =
new java.io.FileOutputStream(new File("output.png"));
BitmapCanvasProvider provider =
new BitmapCanvasProvider(out, "image/x-png", 110,
BufferedImage.TYPE_BYTE_GRAY, false,
0);
bean.generateBarcode(provider, request.getParameter("barcode"));
provider.finish();
BufferedImage barcodeImage = provider.getBufferedImage();
response.setContentType("image/x-png");
OutputStream outputStream = response.getOutputStream();
ImageIO.write(barcodeImage, "png", outputStream);
outputStream.close();
我在文件(examples目录)中找到了一个示例。如果有人在找,我会把它贴在这里。 此外,这里是所有支持的条形码的列表
/*
* Copyright 2004 Jeremias Maerki.
*
* 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.
*/
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
/**
* This example demonstrates creating a bitmap barcode using the bean API.
*
* @author Jeremias Maerki
* @version $Id: SampleBitmapBarcodeWithBean.java,v 1.2 2006/11/07 16:45:28 jmaerki Exp $
*/
public class SampleBitmapBarcodeWithBean {
public static void main(String[] args) {
try {
//Create the barcode bean
Code39Bean bean = new Code39Bean();
final int dpi = 150;
//Configure the barcode generator
bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar
//width exactly one pixel
bean.setWideFactor(3);
bean.doQuietZone(false);
//Open output file
File outputFile = new File("out.jpg");
OutputStream out = new FileOutputStream(outputFile);
try {
//Set up the canvas provider for monochrome JPEG output
BitmapCanvasProvider canvas = new BitmapCanvasProvider(
out, "image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//Generate the barcode
bean.generateBarcode(canvas, "123456");
//Signal end of generation
canvas.finish();
} finally {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
还有一个更高级的示例将自定义元素添加到条形码中
/*
* Copyright 2010 Jeremias Maerki
*
* 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.
*/
/* $Id: SampleBarcodeEnhanced.java,v 1.1 2010/10/05 08:56:04 jmaerki Exp $ */
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.krysalis.barcode4j.impl.datamatrix.DataMatrixBean;
import org.krysalis.barcode4j.impl.datamatrix.SymbolShapeHint;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.output.bitmap.BitmapEncoder;
import org.krysalis.barcode4j.output.bitmap.BitmapEncoderRegistry;
import org.krysalis.barcode4j.tools.UnitConv;
/**
* This class demonstrates how to create a barcode bitmap that is enhanced by custom elements.
*
* @version $Id: SampleBarcodeEnhanced.java,v 1.1 2010/10/05 08:56:04 jmaerki Exp $
*/
public class SampleBarcodeEnhanced {
private void generate(File outputFile) throws IOException {
String msg = "Sample Message";
String[] paramArr = new String[] {"Information 1", "Information 2", "Barcode4J is cool!"};
//Create the barcode bean
DataMatrixBean bean = new DataMatrixBean();
final int dpi = 200;
//Configure the barcode generator
bean.setModuleWidth(UnitConv.in2mm(8.0f / dpi)); //makes a dot/module exactly eight pixels
bean.doQuietZone(false);
bean.setShape(SymbolShapeHint.FORCE_RECTANGLE);
boolean antiAlias = false;
int orientation = 0;
//Set up the canvas provider to create a monochrome bitmap
BitmapCanvasProvider canvas = new BitmapCanvasProvider(
dpi, BufferedImage.TYPE_BYTE_BINARY, antiAlias, orientation);
//Generate the barcode
bean.generateBarcode(canvas, msg);
//Signal end of generation
canvas.finish();
//Get generated bitmap
BufferedImage symbol = canvas.getBufferedImage();
int fontSize = 32; //pixels
int lineHeight = (int)(fontSize * 1.2);
Font font = new Font("Arial", Font.PLAIN, fontSize);
int width = symbol.getWidth();
int height = symbol.getHeight();
FontRenderContext frc = new FontRenderContext(new AffineTransform(), antiAlias, true);
for (int i = 0; i < paramArr.length; i++) {
String line = paramArr[i];
Rectangle2D bounds = font.getStringBounds(line, frc);
width = (int)Math.ceil(Math.max(width, bounds.getWidth()));
height += lineHeight;
}
//Add padding
int padding = 2;
width += 2 * padding;
height += 3 * padding;
BufferedImage bitmap = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g2d = (Graphics2D)bitmap.getGraphics();
g2d.setBackground(Color.white);
g2d.setColor(Color.black);
g2d.clearRect(0, 0, bitmap.getWidth(), bitmap.getHeight());
g2d.setFont(font);
//Place the barcode symbol
AffineTransform symbolPlacement = new AffineTransform();
symbolPlacement.translate(padding, padding);
g2d.drawRenderedImage(symbol, symbolPlacement);
//Add text lines (or anything else you might want to add)
int y = padding + symbol.getHeight() + padding;
for (int i = 0; i < paramArr.length; i++) {
String line = paramArr[i];
y += lineHeight;
g2d.drawString(line, padding, y);
}
g2d.dispose();
//Encode bitmap as file
String mime = "image/png";
OutputStream out = new FileOutputStream(outputFile);
try {
final BitmapEncoder encoder = BitmapEncoderRegistry.getInstance(mime);
encoder.encode(bitmap, out, mime, dpi);
} finally {
out.close();
}
}
/**
* Command-line program.
* @param args the command-line arguments
*/
public static void main(String[] args) {
try {
SampleBarcodeEnhanced app = new SampleBarcodeEnhanced();
File outputFile = new File("out.png");
app.generate(outputFile);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
}
/*
*版权所有2010 Jeremias Maerki
*
*根据Apache许可证2.0版(以下简称“许可证”)获得许可;
*除非遵守许可证,否则不得使用此文件。
*您可以通过以下方式获得许可证副本:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*/
/*$Id:SampleBarcodeEnhanced.java,版本1.1 2010/10/05 08:56:04 jmaerki Exp$*/
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics2D;
导入java.awt.font.FontRenderContext;
导入java.awt.geom.AffineTransform;
导入java.awt.geom.Rectangle2D;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.OutputStream;
导入org.krysalis.barcode4j.impl.datamatrix.DataMatrixBean;
导入org.krysalis.barcode4j.impl.datamatrix.SymbolShapeHint;
导入org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
导入org.krysalis.barcode4j.output.bitmap.BitmapEncoder;
导入org.krysalis.barcode4j.output.bitmap.BitmapEncoderRegistry;
导入org.krysalis.barcode4j.tools.UnitConv;
/**
*此类演示如何创建由自定义元素增强的条形码位图。
*
*@version$Id:SampleBarcodeEnhanced.java,v 1.1 2010/10/05 08:56:04 jmaerki Exp$
*/
公共类SampleBarcodeEnhanced{
私有void generate(File outputFile)引发IOException{
String msg=“示例消息”;
String[]paramArr=新字符串[]{“信息1”、“信息2”、“条形码4j很酷!”;
//创建条形码bean
DataMatrixBean=新DataMatrixBean();
最终整数dpi=200;
//配置条形码生成器
setModuleWidth(UnitConv.in2mm(8.0f/dpi));//使点/模块正好有八个像素
bean.doQuietZone(假);
bean.setShape(SymbolShapeHint.FORCE_矩形);
布尔反别名=假;
int方向=0;
//设置画布提供程序以创建单色位图
BitmapCanvasProvider canvas=新的BitmapCanvasProvider(
dpi、BuffereImage.TYPE_BYTE_BINARY、反别名、方向);
//生成条形码
generateBarcode(canvas,msg);
//信号生成结束
canvas.finish();
//获取生成的位图
BuffereImage symbol=canvas.getBuffereImage();
int fontSize=32;//像素
int线宽=(int)(fontSize*1.2);
Font Font=新字体(“Arial”,Font.PLAIN,fontSize);
int width=symbol.getWidth();
int height=symbol.getHeight();
FontRenderContext frc=新FontRenderContext(新仿射变换(),反别名,真);
for(int i=0;i
使用以下链接下载jar文件:
//
如果您指的是分辨率,那么应该修改
BitmapCanvasProvider
构造函数的第一个参数
/*
// Select a barcode type to create a Java barcode object
Code128 barcode = new Code128();
// Set barcode data text to encode
barcode.setData("Barcode-in-Java");
// Set barcode data text to encode
barcode.setX(2);
// Generate barcode & encode into GIF format
barcode.drawBarcode("C://barcode-code128.gif");
// Generate barcode & encode into JPEG format
barcode.drawBarcode("C://barcode-code128.jpg");
// Generate barcode & encode into EPS format
barcode.drawBarcode2EPS("C://barcode-code128.eps");
// Generate barcode & print into Graphics2D object
barcode.drawBarcode("Java Graphics2D object");
*/
String path=System.getProperty("user.home") "/Desktop";
String val = "val here"
Code39 barcode = new Code39();
barcode.setData(val);
barcode.setX(2);
barcode.drawBarcode(path "/" NIC ".png");