资源简介
用java代码打印出高清图片。非常方便。有效。希望对大家有所帮助
代码片段和文件信息
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.krysalis.barcode4j.BarcodeGenerator;
import org.krysalis.barcode4j.BarcodeUtil;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.MimeTypes;
public class CodeService
{
private static CodeService instance;
private static DefaultConfiguration cfg;
/**
* 二维码参数
*/
private static final String FORMAT = MimeTypes.MIME_JPEG;
private static final int ORIENTATION = 0;
private static final int RESOLUTION = 300;
private static final String BARCODE_TYPE = “datamatrix“;
private CodeService()
{
}
public static CodeService getInstance()
{
if (instance == null)
{
instance = new CodeService();
cfg = new DefaultConfiguration(“barcode“);
DefaultConfiguration child = new DefaultConfiguration(BARCODE_TYPE);
cfg.addChild(child);
DefaultConfiguration attr;
attr = new DefaultConfiguration(“height“);
attr.setValue(50);
child.addChild(attr);
attr = new DefaultConfiguration(“module-width“);
attr.setValue(“0.6“);
child.addChild(attr);
}
return instance;
}
public InputStream getCodeImage(String code String type)
{
InputStream ret = null;
ByteArrayOutputStream baos = null;
BitmapCanvasProvider bitmap = null;
try
{
Configuration cfg = buildCfg(type);
BarcodeUtil util = BarcodeUtil.getInstance();
BarcodeGenerator gen = util.createBarcodeGenerator(cfg);
baos = new ByteArrayOutputStream(4096);
bitmap = new BitmapCanvasProvider(baos FORMAT RESOLUTION
BufferedImage.TYPE_BYTE_GRAY true ORIENTATION);
gen.generateBarcode(bitmap code);
bitmap.finish();
ByteArrayInputStream st = new ByteArrayInputStream(baos.toByteArray());
ret = st;
} catch (Exception e)
{
e.printStackTrace();
} finally
{
try
{
if (baos != null)
{
baos.close();
}
bitmap = null;
} catch (Exception e)
{
}
}
return ret;
}
private Configuration buildCfg(String type)
{
return cfg;
}
public static final byte[] input2byte(InputStream inStream)
throws IOException {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = inStream.read(buff 0 100)) > 0) {
swapStream.write(buff 0 rc);
}
byte[] in2b = swapStream.toByteArray();
return in2b;
}
public static void main(String[] args) {
File outputFile = new File(“d:\\out.jp
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-08-20 10:23 printer\
文件 1276 2015-08-20 10:23 printer\.classpath
文件 383 2015-08-20 10:23 printer\.project
目录 0 2015-08-20 10:23 printer\.settings\
文件 598 2015-08-20 10:23 printer\.settings\org.eclipse.jdt.core.prefs
目录 0 2015-08-21 16:21 printer\bin\
文件 4187 2015-08-21 16:21 printer\bin\CodeService.class
文件 2314 2015-08-21 16:21 printer\bin\EAN.class
文件 965 2015-08-21 16:21 printer\bin\JPEGImageTest.class
文件 3095 2015-08-21 16:21 printer\bin\MyPrintTest.class
文件 3062 2015-08-21 16:21 printer\bin\Printer1.class
文件 1129 2015-08-21 16:21 printer\bin\PrinterTest$1.class
文件 1079 2015-08-21 16:21 printer\bin\PrinterTest.class
文件 4892 2015-08-21 16:21 printer\bin\SampleBarcodeEnhanced.class
文件 1737 2015-08-21 16:21 printer\bin\SampleBitmapBarcodeWithBean.class
文件 3449 2015-08-21 16:21 printer\bin\Test4.class
目录 0 2015-08-21 16:21 printer\bin\core\
文件 2725 2015-08-21 16:21 printer\bin\core\PrintTest.class
文件 3265 2015-08-21 16:21 printer\bin\core\PrintTest1.class
目录 0 2015-08-21 16:21 printer\bin\img\
文件 3008 2015-08-21 16:21 printer\bin\img\PrintImage.class
文件 4464 2015-08-21 16:21 printer\bin\img\PrintSalesSlip.class
目录 0 2015-08-21 16:21 printer\bin\pdf\
文件 2240 2015-08-21 16:21 printer\bin\pdf\PrintPDF.class
文件 721 2015-08-21 16:21 printer\bin\pdf\PrintUtil.class
目录 0 2015-08-21 16:21 printer\bin\test\
文件 2756 2015-08-21 16:21 printer\bin\test\PrintImage.class
文件 2148 2015-08-21 16:21 printer\bin\test\Test1.class
目录 0 2015-08-21 16:21 printer\bin\tiff\
文件 1096 2015-08-21 16:21 printer\bin\tiff\PrintTiff$MyPrintable.class
文件 1377 2015-08-21 16:21 printer\bin\tiff\PrintTiff.class
............此处省略43个文件信息
评论
共有 条评论