将已经存入到FastFDS中的图片压缩:
首先从数据库中取出FastFDS返回的地址,然后将FastFDS返回的地址转换成推案文件
测试类
package com.msp.whg.service;
import com.msp.whg.domain.InfoManage;
import com.msp.whg.util.AppUUID;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by Administrator on 2018\2\1 0001.
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class test {
@Autowired
private InfoManageService infoManageService;
public void transformer(File srcImageFile,int maxPixel) {
String tarImage = "C:\\Users\\Administrator\\Desktop\\2015101713_720_720.jpg";
//源图片文件
//File srcImageFile = new File(srcImage);
//目的图片文件
File tarImageFile = new File(tarImage);
// 生成图片转化对象
AffineTransform transform = new AffineTransform();
// 通过缓存读入缓存对象
BufferedImage image = null;
try {
image = ImageIO.read(srcImageFile);
} catch (IOException e) {
e.printStackTrace();
}
int imageWidth = image.getWidth();//原图片的高度
int imageHeight = image.getHeight();//原图片的宽度
int changeWidth = 0;//压缩后图片的高度
int changeHeight = 0;//压缩后图片的宽度
double scale = 0;// 定义小图片和原图片比例
if (maxPixel != 0) {
if (imageWidth > imageHeight) {
changeWidth = maxPixel;
scale = (double) changeWidth / (double) imageWidth;
changeHeight = (int) (imageHeight * scale);
} else {
changeHeight = maxPixel;
scale = (double) changeHeight / (double) imageHeight;
changeWidth = (int) (imageWidth * scale);
}
}
// 生成转换比例
transform.setToScale(scale, scale);
// 生成转换操作对象
AffineTransformOp transOp = new AffineTransformOp(transform, null);
//生成压缩图片缓冲对象
BufferedImage basll = new BufferedImage(changeWidth, changeHeight, BufferedImage.TYPE_3BYTE_BGR);
//生成缩小图片
transOp.filter(image, basll);
try {
//输出缩小图片
ImageIO.write(basll, "jpeg",tarImageFile);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(tarImage+"======================");
InfoManage infoManage = new InfoManage();
infoManage.setImLmmc("增加测试11111");
infoManage.setImTitle("标题");
infoManage.setBanner("1");
infoManage.setImPath(tarImage);
infoManageService.add(infoManage);
}
@Test
public void InfoManageAdd(){
String srcImage = "http://192.168.100.169/group1/M00/00/09/wKhkqVxAtBeAFg_yACBpQt4xiEo129.jpg";
File file1 = null;
byte[] btImg = getImageFromNetByUrl(srcImage);
if(null != btImg && btImg.length > 0){
System.out.println("读取到:" + btImg.length + " 字节");
String fileName = "百度.gif";
file1 = writeImageToDisk(btImg, fileName);
}else{
System.out.println("没有从该连接获得内容");
}
int maxPixel = 1220 ;
transformer(file1,maxPixel);
}
/**
* 将图片写入到磁盘
* @param img 图片数据流
* @param fileName 文件保存时的名称
*/
public File writeImageToDisk(byte[] img, String fileName){
File file = null;
try {
file = new File("C:\\Users\\Administrator\\Desktop\\" + fileName);
FileOutputStream fops = new FileOutputStream(file);
fops.write(img);
fops.flush();
fops.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
/**
* 根据地址获得数据的字节流
* @param strUrl 网络连接地址
* @return
*/
public byte[] getImageFromNetByUrl(String strUrl){
try {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
byte[] btImg = readInputStream(inStream);//得到图片的二进制数据
return btImg;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 从输入流中获取数据
* @param inStream 输入流
* @return
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}