您的位置:首页 > Web前端

java 实现将图片低端,添加文字.并压缩打包,提供给前端下载到本地.

2017-09-18 16:39 579 查看
          项目中,有提供照片上传的功能,现在用户需要导出图片,但需要在照片底部添加用户名和编号,基本参考有一篇博文:可以使用java内置的图形工具,建立画布,将图片绘制在画布上,然后在底部添加文字,工具类如下:

package test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.springMVC.util.LogUtil;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/*
* 导出用户照片,添加编号和姓名在底部,2017年9月18日16:08:27
*/
public class ChartGraphics {
private BufferedImage image;
private int imageWidth = 400; // 图片的宽度
private int imageHeight = 480; // 图片的高度

public void graphicsGeneration(String imgurl) {
int H_tip = 60; // 文字的高度

image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
// 设置图片的背景色
Graphics2D main = image.createGraphics();
main.setColor(Color.white);
main.fillRect(0, 0, imageWidth, imageHeight);

// ***********************用户照片**************
Graphics mainPic = image.getGraphics();
BufferedImage bimg = null;
try {
bimg = javax.imageio.ImageIO.read(new java.io.File(imgurl));
} catch (Exception e) {
}

if (bimg != null) {
mainPic.drawImage(bimg, 0, 0, 400, 400, null);
mainPic.dispose();
}
// ***********************设置最下方的用户编号和姓名****************

Graphics2D tip = image.createGraphics();
// 设置区域颜色
tip.setColor(Color.white);
// 填充区域并确定区域大小位置
tip.fillRect(0, 400, 400, H_tip);
// 设置字体颜色,先设置颜色,再填充内容
tip.setColor(Color.black);
// 设置字体
Font tipFont = new Font("宋体", Font.BOLD, 17);
tip.setFont(tipFont);
// tip.drawString("2016rz0001+王银生", 120, 450);
tip.drawString("新添加的标题", 120, 450);

createImage("E:\\person.png");
System.out.println("生成图片成功");

}

// 生成图片文件
@SuppressWarnings("restriction")
public void createImage(String fileLocation) {
BufferedOutputStream bos = null;
if (image != null) {
try {
FileOutputStream fos = new FileOutputStream(fileLocation);
bos = new BufferedOutputStream(fos);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {// 关闭输出流
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

public static void main(String[] args) {
ChartGraphics cg = new ChartGraphics();
try {
cg.graphicsGeneration("E:\\1.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
}


           参考博客地址:http://www.cnblogs.com/qianna-daisy/p/4572221.html,在他的基础上稍微修改了一点,谢谢楼主.运行截图如下:

       原图:


      添加文字后效果图:

    




****************************************分割线****************************

     假设用户一下子选择了多个用户照片,需要同时下载,这时候就需要用到压缩文件,先转换文件,然后再压缩,使用a标签,让前端进行下载,压缩工具类如下,参考如下博文http://www.cnblogs.com/xgjblog/p/3807907.html::

package com.rmsClient.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.rmsClient.entity.dto.UserPicture;

public class TransferToZipUtil {

public static void transToZip(File file, String fileName, List<UserPicture> userPicturesList) throws IOException {

byte[] buffer = new byte[1024];

file = new File(file.getAbsolutePath() + File.separator + fileName + ".zip");

// 生成的ZIP文件名为Demo.zip
// String strZipName = "D:\\Demo.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));

// 需要同时下载的多个照片文件
List<File> manyFiles = new ArrayList<File>();

for (int i = 0; i < userPicturesList.size(); i++) {
// http://192.168.40.55:8080/upload/10141505718079.png // 需要使用本地的地址,转成服务端的地址D盘下的picture目录
if (userPicturesList.get(i).getPicture().length() > 0) {
String urlPictureUrl = "D:\\用户照片\\" + userPicturesList.get(i).getRetireInforId()
+ userPicturesList.get(i).getName() + ".png";
manyFiles.add(new File(urlPictureUrl));
}

}

// 将文件进行压缩
for (int i = 0; i < manyFiles.size(); i++) {
FileInputStream fis = new FileInputStream(manyFiles.get(i));
out.putNextEntry(new ZipEntry(manyFiles.get(i).getName()));
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
System.out.println("生成照片压缩文件成功");

}
}
      前端可以使用:
 $("#dlink").attr("href",json.content);
$("#dlink").attr("download",'用户照片'+'.zip');
document.getElementById("dlink").click();

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图片 java
相关文章推荐