java以流的形式将文字写在图片上
来源:互联网 发布:国外数据分析公司 编辑:程序博客网 时间:2024/05/24 22:44
图片工具类,将一些文字通过java输出到图片上,然后在前端以图片形式展示就显稍微高端一些哈。
废话少说,上图
PictureUtil.java
package cn.hx.as.util;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class PictureUtil { private static void createImage(String fileLocation, BufferedImage image) { try { FileOutputStream fos = new FileOutputStream(fileLocation); BufferedOutputStream bos = new BufferedOutputStream(fos); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos); encoder.encode(image); bos.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } // // public static void graphicsGeneration(String path, PolicyBean policyBean) // { // // int imageWidth = 1000;// 图片的宽度 // // int imageHeight = 1000;// 图片的高度 // // BufferedImage image = new BufferedImage(imageWidth, imageHeight, // BufferedImage.TYPE_INT_RGB); // Graphics graphics = image.getGraphics(); // graphics.setColor(Color.white); // graphics.fillRect(0, 0, imageWidth, imageHeight); // graphics.setColor(Color.ORANGE); // graphics.setFont(new Font("宋体", Font.BOLD, 20)); // // // PolicyPersonDto policyPersonDto = policyBean.getPolicyPersonDto(); // // PolicyProductDto policyProductDto = policyBean.getPolicyProductDto(); // // int num = 50; // graphics.drawString("手机号 : " + policyPersonDto.getMobile(), 50, // num); // num += 50; // graphics.drawString("登陆密码 : " + policyPersonDto.getPassword(), 50, // num); // num += 50; // graphics.drawString("身份证号 : " + policyPersonDto.getIdentityCard(), 50, // num); // num += 50; // graphics.drawString("姓名 : " + policyPersonDto.getUserName(), 50, // num); // num += 50; // graphics.drawString("所在地区 : " + policyPersonDto.getArea(), 50, num); // num += 50; // graphics.drawString("常住地址 : " + policyPersonDto.getAddress(), 50, // num); // num += 50; // graphics.drawString("微信号 : " + policyPersonDto.getWechatNo(), 50, // num); // num += 50; // graphics.drawString("QQ号 : " + policyPersonDto.getQq(), 50, num); // num += 50; // graphics.drawString("Email : " + policyPersonDto.getEmail(), 50, // num); // num += 50; // graphics.drawString("品牌 : " + policyProductDto.getBrand(), 50, // num); // num += 50; // graphics.drawString("型号 : " + policyProductDto.getModel(), 50, // num); // num += 50; // graphics.drawString("商品编号 : " + policyProductDto.getProductId(), 50, // num); // num += 50; // graphics.drawString("购买日期 : " + policyProductDto.getPurchaseDate(), // 50, num); // num += 50; // graphics.drawString("购买价格 : " + policyProductDto.getPurchasePrice(), // 50, num); // num += 50; // graphics.drawString("购买途径 : " + policyProductDto.getPurchaseWay(), 50, // num); // num += 50; // graphics.drawString("报修电话 : " + policyProductDto.getWarrantyPhone(), // 50, num); // num += 50; // graphics.drawString("延保单位 : " + // policyProductDto.getExtendedWarrantyUnit(), 50, num); // num += 50; // graphics.drawString("延保电话 : " + // policyProductDto.getExtendedWarrantyPhone(), 50, num); // num += 50; // graphics.drawString("发票编号 : " + policyProductDto.getInvoiceNo(), 50, // num); // // createImage(path, image); // } public static void main(String[] args) { List<Map> list = new ArrayList<Map>(); Map<String, String> mapTitle1 = new HashMap<String, String>(); mapTitle1.put("title", "使用人信息"); list.add(mapTitle1); Map<String, String> map1 = new HashMap<String, String>(); map1.put("客户姓名", "张三"); map1.put("手机号", "123123"); map1.put("身份证号", "230302198811241234"); list.add(map1); Map<String, String> map2 = new HashMap<String, String>(); map2.put("送货地址", "北京市海淀区知春路113号银网中心B座1009室"); list.add(map2); Map<String, String> map3 = new HashMap<String, String>(); map3.put("微信号码", "123123"); map3.put("qq号码", "123123"); map3.put("电子邮箱", "gaop@haierubic.com"); list.add(map3); Map<String, String> mapTitle2 = new HashMap<String, String>(); mapTitle2.put("title", "购买人信息"); list.add(mapTitle2); Map<String, String> map4 = new HashMap<String, String>(); map4.put("姓名", "张三朋友"); map4.put("手机号", "15612341122"); map4.put("身份证号", "230302198811241234"); list.add(map4); Map<String, String> mapTitle3 = new HashMap<String, String>(); mapTitle3.put("title", "产品信息"); list.add(mapTitle3); Map<String, String> map5 = new HashMap<String, String>(); map5.put("产品型号", "ALI88"); map5.put("凭证类型", "发票"); map5.put("购买日期", "2014-12-02"); list.add(map5); Map<String, String> map6 = new HashMap<String, String>(); map6.put("购买商城", "ALI88"); map6.put("凭证编号", "发票"); list.add(map6); int imageWidth = 1200;// 图片的宽度 int imageHeight = 1000;// 图片的高度 BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); Graphics graphics = image.getGraphics(); graphics.setColor(Color.white); graphics.fillRect(0, 0, imageWidth, imageHeight); graphics.setColor(Color.black); int high = 100; int wigth = 0; graphics.setFont(new Font("宋体", Font.BOLD, 50)); graphics.drawString("注册保单", 500, high); graphics.setFont(new Font("宋体", Font.BOLD, 20)); high += 10; graphics.drawLine(50, high, 1150, high); for (Map<String, String> rowMap : list) { high += 50; wigth = 50; for (Map.Entry<String, String> entry : rowMap.entrySet()) { String name = entry.getKey() + ":" + entry.getValue(); if ("title".equals(entry.getKey())) { high += 50; graphics.setFont(new Font("黑体", Font.BOLD, 30)); graphics.drawString(entry.getValue(), wigth, high); graphics.setFont(new Font("宋体", Font.BOLD, 20)); } else { graphics.drawString(name, wigth, high); wigth += 400; } } } createImage("D://test1.jpg", image); }}
阅读全文
0 0
- java以流的形式将文字写在图片上
- php将文字写在图片上,图片加水印
- java将文字写在图片上,并做成压缩包导出到客户端
- java获取一段文字的url地址并以链接的形式显示在网页上
- JAVA将图片以二进制形式存入Access数据库
- js以字符串的形式将图片上传到服务器
- 以二进制流的形式上传图片
- visual c++将图片以二进制流的形式存入到mysql中
- 将整数以文字的形式输出。如-1,025,050输出为:负壹佰零贰万伍仟零伍拾
- Java调用Linux下的shell命令并将结果以流的形式返回
- java 后台把数据以图片的形式传到前台
- 以文字流的方式下载图片
- word2010以嵌入文字下面的形式插入的图片如何选中
- css3实现从左右两边以动画的形式分别插入文字和图片
- java以流的形式显示文件
- java以流的形式输出文件
- 复制图片上的文字,通过Microsoft Onenote将图片上的文字复制出来
- 将C# Web Service返回的图片以流的形式显示在Delphi中的Image控件
- HTML常用标签(1)
- Linux 安装和环境配置
- 11月9日笔记
- luogu1063【06提高】能量项链(区间dp)
- metasploit 情报搜集技术【4】网络漏洞扫描
- java以流的形式将文字写在图片上
- 20171019每日一练
- Javascript测试框架Jasmine(四):自定义Matcher
- SHUOJ 2333
- ubuntu安装mysql可视化工具MySQL-workbench及简单操作
- caffe中不同层的含义和卷积的计算方式
- 泊松融合-Possion Blending
- Javascript测试框架Jasmine(五):Spies
- Keras如何构造简单的CNN网络