|
时 间 记 忆 |
<< < 2018 - 2 > >>
日 |
一 |
二 |
三 |
四 |
五 |
六 |
|
|
|
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
|
|
|
|
|
|
|
|
|
java 生成条形码 |
[ 2017-2-28 13:14:00 | By: 我家超超会发光 ] |
package com.hljw.cmeav.service.record;
import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.imageio.ImageIO;
import org.apache.commons.lang.StringUtils; import org.krysalis.barcode4j.HumanReadablePlacement; import org.krysalis.barcode4j.impl.code128.Code128Bean; import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider; import org.krysalis.barcode4j.tools.MimeTypes; import org.krysalis.barcode4j.tools.UnitConv; import org.springwork.beans.factory.annotation.Autowired; import org.springwork.stereotype.Service; import com.google.common.collect.Lists; import com.hljw.base.service.BaseService; import com.hljw.cmeav.pojo.Archive; import com.hljw.cmeav.pojo.ItemGroup; import com.hljw.cmeav.pojo.Record; import com.hljw.cmeav.pojo.RecordBarcode; import com.hljw.cmeav.service.archive.ArchiveService; import com.hljw.cmeav.service.itemgroup.ItemGroupService; import com.hljw.cmeav.service.recordbarcode.RecordBarcodeService; import com.hljw.cmeav.util.CmeavGlobal;
@Service public class BarcodeService extends BaseService {
@Autowired private RecordService recordService;
@Autowired private ArchiveService archiveService;
@Autowired private ItemGroupService itemGroupService; @Autowired private RecordBarcodeService recordBarcodeService;
public String createThisBarCode(String code, String prefix) throws Exception { String path = prefix + code + ".jpg"; Code128Bean bean = new Code128Bean(); final int dpi = 150; bean.setModuleWidth(UnitConv.in2mm(3f / dpi)); bean.doQuietZone(true); bean.setMsgPosition(HumanReadablePlacement.HRP_BOTTOM);// 数字位置 OutputStream out = null; try { out = new FileOutputStream(path); BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, MimeTypes.MIME_JPEG, dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0); bean.generateBarcode(canvas, code); canvas.finish(); out.close(); } catch (Exception e) { e.printStackTrace(); } return path; } public List<String> createBarCode(Map<String, String> groupTableMap, Map<String, String> groupAloneTableMap, String codes, String prefix) throws Exception { List<String> l = Lists.newArrayList(); String[] codeArray = codes.split(","); if (1 == codeArray.length) { RecordBarcode barCode = this.recordBarcodeService.findById(codeArray[0]); if (null != barCode) { Record record = this.recordService.findById(barCode.getRecord_id()); String bottomText = groupTableMap.get(barCode.getBar_code()); String bottomTextAlone = groupAloneTableMap.get(barCode.getBar_code()); Archive archive = this.archiveService.findByRecordId(barCode.getRecord_id()); String topText = archive.getReal_name() + " " + CmeavGlobal.getSexMap(archive.getSex()) + " " + barCode.getRecord_id() + " " + record.getAge() + "岁"; String path = prefix + barCode.getBar_code() + ".jpg"; if(StringUtils.isNotBlank(bottomText)){ path = prefix + barCode.getBar_code() + "_0"+ ".jpg"; generateCode(path, barCode.getBar_code(), topText, bottomText); } if(StringUtils.isNotBlank(bottomTextAlone)){ path = prefix + barCode.getBar_code() + "_1"+ ".jpg"; generateCode(path, barCode.getBar_code(), topText, bottomTextAlone); } l.add(path); } } else { for (String code : codeArray) { RecordBarcode barCode = this.recordBarcodeService.findById(code); String path = prefix + barCode.getBar_code() + ".jpg"; if (null != barCode) { Record record = this.recordService.findById(barCode.getRecord_id()); String bottomText = groupTableMap.get(barCode.getBar_code()); Archive archive = this.archiveService.findByRecordId(barCode.getRecord_id()); String topText = archive.getReal_name() + " " + CmeavGlobal.getSexMap(archive.getSex()) + " " + barCode.getRecord_id() + " " + record.getAge() + "岁"; generateCode(path, barCode.getBar_code(), topText, bottomText); l.add(path); } } } return l; }
public List<String> createBarCode(String groupId, String recordId, String prefix) { List<String> l = Lists.newArrayList(); ItemGroup itemGroup = this.itemGroupService.findById(groupId); String path = prefix + groupId + "_1.jpg"; Record record = this.recordService.findById(recordId); String bottomText = itemGroup.getGroup_name(); Archive archive = this.archiveService.findByRecordId(recordId); String topText = archive.getReal_name() + " " + CmeavGlobal.getSexMap(archive.getSex()) + " " + recordId + " " + record.getAge() + "岁"; generateCode(path, recordId, topText, bottomText); l.add(path); return l; }
public static void generateCode(String filePath, String code, String topText, String bottomText) { File file = new File(filePath); Code128Bean bean = new Code128Bean(); final int dpi = 150; bean.setModuleWidth(UnitConv.in2mm(3f / dpi)); bean.doQuietZone(true); bean.setMsgPosition(HumanReadablePlacement.HRP_BOTTOM);// 数字位置 OutputStream out = null; try { out = new FileOutputStream(file); BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, MimeTypes.MIME_JPEG, dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0); bean.generateBarcode(canvas, code); canvas.finish(); out.close(); BufferedImage image = ImageIO.read(file); int w = image.getWidth(); int h = image.getHeight(); BufferedImage bi = new BufferedImage(w, h + 50, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) bi.getGraphics(); g.fillRect(0, 0, w, h + 55); Font font = new Font("新宋体", Font.PLAIN, 18); g.drawImage(image, 0, 25, null); g.setFont(font); g.setBackground(Color.white); g.setColor(Color.black); g.drawString(topText, 8, 15); g.setFont(font); g.setBackground(Color.white); g.setColor(Color.black); g.drawString(bottomText, 8, 20 + h + 22); g.dispose(); ImageIO.write(bi, "jpg", file); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * 将宽度相同的图片,竖向追加在一起 ##注意:宽度必须相同 * * @param piclist * 文件路径列表 * @param outPath * 输出路径 */ public void mergeImage(List<String> piclist, String outPath) {// 纵向处理图片 if (piclist == null || piclist.size() <= 0) { System.out.println("图片数组为空!"); return; } try { int height = 0, // 总高度 width = 0, // 总宽度 _height = 0, // 临时的高度 , 或保存偏移高度 __height = 0, // 临时的高度,主要保存每个高度 picNum = piclist.size();// 图片的数量 File fileImg = null; // 保存读取出的图片 int[] heightArray = new int[picNum]; // 保存每个文件的高度 BufferedImage buffer = null; // 保存图片流 List<int[]> imgRGB = new ArrayList<int[]>(); // 保存所有的图片的RGB int[] _imgRGB; // 保存一张图片中的RGB数据 for (int i = 0; i < picNum; i++) { fileImg = new File(piclist.get(i)); buffer = ImageIO.read(fileImg); heightArray[i] = _height = buffer.getHeight();// 图片高度 if (i == 0) { width = buffer.getWidth();// 图片宽度 } height += _height; // 获取总高度 _imgRGB = new int[width * _height];// 从图片中读取RGB _imgRGB = buffer.getRGB(0, 0, width, _height, _imgRGB, 0, width); imgRGB.add(_imgRGB); } _height = 0; // 设置偏移高度为0 // 生成新图片 BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < picNum; i++) { __height = heightArray[i]; if (i != 0) _height += __height; // 计算偏移高度 imageResult.setRGB(0, _height, width, __height, imgRGB.get(i), 0, width); // 写入流中 } File outFile = new File(outPath); ImageIO.write(imageResult, "jpg", outFile);// 写图片 } catch (Exception e) { e.printStackTrace(); } } public static void generateCode(String filePath, String code, String topText, List<String> bottomTexts) { File file = new File(filePath); Code128Bean bean = new Code128Bean(); final int dpi = 150; bean.setModuleWidth(UnitConv.in2mm(3f / dpi)); bean.doQuietZone(true); bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);// 数字位置 OutputStream out = null; try { out = new FileOutputStream(file); BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, MimeTypes.MIME_JPEG, dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0); bean.generateBarcode(canvas, code); canvas.finish(); out.close(); BufferedImage image = ImageIO.read(file); int w = image.getWidth(); int h = image.getHeight(); BufferedImage bi = new BufferedImage(w, h + 120, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) bi.getGraphics(); g.fillRect(0, 0, w, h + 120); Font font = new Font("新宋体", Font.PLAIN, 15); g.drawImage(image, 0, 20, null); g.setFont(font); g.setBackground(Color.white); g.setColor(Color.black); g.drawString(topText, 8, 15);
int index = 0; for (String bottomText : bottomTexts) { index++; g.setFont(font); g.setBackground(Color.white); g.setColor(Color.black); g.drawString(bottomText, 8, 20 + h + index * 26); } g.dispose(); ImageIO.write(bi, "jpg", file); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { try { /*List<String> bottomTexts = Lists.newArrayList(); bottomTexts.add("体检序号:1609260001"); bottomTexts.add("姓名:陈磊 性别:男 年龄:34岁"); bottomTexts.add("单位:西北工业大学");*/ generateCode("D:/1609260001.jpg", "1609260001", "张三 女 23", "乙肝五项 乙肝五项 乙肝五项"); } catch (Exception e) { e.printStackTrace(); } } }
需要下载的包:barcode4j.jar |
|
|
|
|