Pārlūkot izejas kodu

--update ImagesService.resImage()

zzy.zhiyuan.foxmail 8 gadi atpakaļ
vecāks
revīzija
6358b06861

+ 10 - 24
src/main/java/com/ekexiu/portal/service/ImagesService.java

@ -1,7 +1,5 @@
1 1
package com.ekexiu.portal.service;
2 2

3
import java.awt.geom.AffineTransform;
4
import java.awt.image.AffineTransformOp;
5 3
import java.awt.image.BufferedImage;
6 4
import java.io.ByteArrayInputStream;
7 5
import java.io.ByteArrayOutputStream;
@ -39,6 +37,7 @@ public class ImagesService {
39 37
	private File tmpPath;
40 38
	private File resourcePath;
41 39
	private int resImageMaxLen = 240;
40
	private static final String JPG = "jpg";
42 41
	@Autowrie
43 42
	private ImageDao imageDao;
44 43
	
@ -74,22 +73,10 @@ public class ImagesService {
74 73
		this.imageDao = imageDao;
75 74
	}
76 75
	
77
	/**
78
	 * 图片按比例缩放
79
	 * @param src 原图片
80
	 * @param maxLen 最长一边的长度
81
	 * @return 返回缩放后的图片
82
	 * @throws IOException
83
	 */
84
	public static byte[] resImage(byte[] src, int maxLen) throws IOException{
76
	private byte[] resImage(byte[] src, int maxLen) throws IOException {
85 77
		ByteArrayInputStream in = new ByteArrayInputStream(src);
86 78
		ByteArrayOutputStream out = new ByteArrayOutputStream();
87
		BufferedImage bi = ImageIO.read(in);
88
	    double max = bi.getWidth()>bi.getHeight()?bi.getWidth():bi.getHeight();
89
	    double scaling = maxLen/max;
90
	    AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(scaling,scaling), null);
91
		java.awt.Image Itemp = ato.filter(bi, null);
92
		ImageIO.write((BufferedImage) Itemp, "jpg", out);
79
		JpgUtil.scalingZoom(in, out, maxLen);
93 80
		out.flush();
94 81
		return out.toByteArray();
95 82
	}
@ -114,16 +101,15 @@ public class ImagesService {
114 101
			throws SQLException, IOException, JfwBaseException {
115 102
		byte[] src = this.readTmpFile(fn);
116 103
		src = JpgUtil.read(src);
117
		byte[] shareResImage = resImage(src, this.resImageMaxLen);
118
		IoUtil.saveStream(new FileOutputStream(new File(this.resourcePath, resourceId + ".jpg")), src, true);
119
		IoUtil.saveStream(new FileOutputStream(new File(this.resourcePath, resourceId + "_s.jpg")), shareResImage, true);
120
		
104
		byte[] shareResImage = this.resImage(src, this.resImageMaxLen);
105
		IoUtil.saveStream(new FileOutputStream(new File(this.resourcePath, resourceId + "." + JPG)), src, true);
106
		IoUtil.saveStream(new FileOutputStream(new File(this.resourcePath, resourceId + "_s." + JPG)), shareResImage, true);
121 107
		Image image1 = this.imageDao.queryOne(con, resourceId);
122 108
		if(null == image1){
123 109
			Image image = new Image();
124 110
			String imageId = StringUtil.buildUUID();
125 111
			image.setImageId(imageId);
126
			String imageSrc = this.resourcePath + "/" + resourceId + ".jpg";
112
			String imageSrc = this.resourcePath + "/" + resourceId + "." + JPG;
127 113
			image.setImageSrc(imageSrc);
128 114
			image.setResourceId(resourceId);
129 115
			this.imageDao.insert(con, image);
@ -138,8 +124,8 @@ public class ImagesService {
138 124
	public String insertResourceImg(@JdbcConn(true) Connection con, String resourceId, String base64, Integer angle) throws SQLException, IOException {
139 125
		Base64 bs64 = new Base64();
140 126
		byte[] bs = bs64.decode(base64.getBytes("UTF-8"));
141
		String imageSrc = this.resourcePath + "/" + resourceId + ".jpg";
142
		String resImage = this.resourcePath + "/" + resourceId + "_s.jpg";
127
		String imageSrc = this.resourcePath + "/" + resourceId + "." + JPG;
128
		String resImage = this.resourcePath + "/" + resourceId + "_s." + JPG;
143 129
		FileOutputStream fos = new FileOutputStream(imageSrc);
144 130
		FileOutputStream fos2 = new FileOutputStream(resImage);
145 131
		try {
@ -152,7 +138,7 @@ public class ImagesService {
152 138
				BufferedImage des = PictureRotating.Rotate(image, angle);
153 139
				ByteArrayOutputStream out = new ByteArrayOutputStream();
154 140
				@SuppressWarnings("unused")
155
				boolean flag = ImageIO.write(des, "jpg", out);
141
				boolean flag = ImageIO.write(des, JPG, out);
156 142
				byte[] bytes = out.toByteArray();
157 143
				byte[] bs2 = resImage(bytes, this.resImageMaxLen);
158 144
				fos.write(bytes);