Browse Source

资源图片保存的接口添加图片旋转保存的功能。

zzy.zhiyuan.foxmail 8 years ago
parent
commit
1c17b17ce1

+ 17 - 24
src/main/java/com/ekexiu/portal/service/ImageService.java

@ -1,6 +1,5 @@
1 1
package com.ekexiu.portal.service;
2 2
3
import java.awt.Image;
4 3
import java.awt.image.BufferedImage;
5 4
import java.io.ByteArrayInputStream;
6 5
import java.io.ByteArrayOutputStream;
@ -14,7 +13,6 @@ import java.util.concurrent.TimeUnit;
14 13
15 14
import javax.imageio.ImageIO;
16 15
17
import org.jfw.apt.annotation.Nullable;
18 16
import org.jfw.apt.web.annotation.Path;
19 17
import org.jfw.apt.web.annotation.operate.Get;
20 18
import org.jfw.apt.web.annotation.operate.Post;
@ -319,30 +317,25 @@ public class ImageService {
319 317
	
320 318
	@Post
321 319
	@Path("/saveHead")
322
	public boolean saveImage(String id, String base64) throws IOException {
320
	public boolean saveImage(String id, String base64, Integer angle) throws IOException {
323 321
		Base64 bs64 = new Base64();
324 322
		byte[] bs = bs64.decode(base64.getBytes("UTF-8"));
325
		byte[] li = this.zoomImage(bs, this.largeHeadPhotoWidth, this.largeHeadPhotoHeight);
326
		byte[] mi = this.zoomImage(bs, this.middleHeadPhotoWidth, this.middleHeadPhotoHeight);
327
		byte[] si = this.zoomImage(bs, this.smallHeadPhotoWidth, this.smallHeadPhotoHeight);
328
		IoUtil.saveStream(new FileOutputStream(new File(this.headPath, id + "_s.jpg")), si, true);
329
		IoUtil.saveStream(new FileOutputStream(new File(this.headPath, id + "_m.jpg")), mi, true);
330
		IoUtil.saveStream(new FileOutputStream(new File(this.headPath, id + "_l.jpg")), li, true);
331
		return true;
332
	}
333
	@Post
334
	@Path("/test")
335
	public boolean Image(String id, String base64, @Nullable Integer angle) throws IOException {
336
		Base64 bs64 = new Base64();
337
		byte[] bs = bs64.decode(base64.getBytes("UTF-8"));
338
	    Image image = PictureRotating.bytesToImage(bs);
339
	    BufferedImage des = PictureRotating.Rotate(image, angle);
340
	    ByteArrayOutputStream out = new ByteArrayOutputStream();
341
        boolean flag = ImageIO.write(des, "jpg", out);
342
        byte[] bytes = out.toByteArray();
343
		byte[] li = this.zoomImage(bytes, this.largeHeadPhotoWidth, this.largeHeadPhotoHeight);
344
		byte[] mi = this.zoomImage(bytes, this.middleHeadPhotoWidth, this.middleHeadPhotoHeight);
345
		byte[] si = this.zoomImage(bytes, this.smallHeadPhotoWidth, this.smallHeadPhotoHeight);
323
		byte[] li,mi,si;
324
		if(0 == angle){
325
			li = this.zoomImage(bs, this.largeHeadPhotoWidth, this.largeHeadPhotoHeight);
326
			mi = this.zoomImage(bs, this.middleHeadPhotoWidth, this.middleHeadPhotoHeight);
327
			si = this.zoomImage(bs, this.smallHeadPhotoWidth, this.smallHeadPhotoHeight);
328
		}else{
329
			java.awt.Image image = PictureRotating.bytesToImage(bs);
330
			BufferedImage des = PictureRotating.Rotate(image, angle);
331
			ByteArrayOutputStream out = new ByteArrayOutputStream();
332
			@SuppressWarnings("unused")
333
			boolean flag = ImageIO.write(des, "jpg", out);
334
			byte[] bytes = out.toByteArray();
335
			li = this.zoomImage(bytes, this.largeHeadPhotoWidth, this.largeHeadPhotoHeight);
336
			mi = this.zoomImage(bytes, this.middleHeadPhotoWidth, this.middleHeadPhotoHeight);
337
			si = this.zoomImage(bytes, this.smallHeadPhotoWidth, this.smallHeadPhotoHeight);
338
		}
346 339
		IoUtil.saveStream(new FileOutputStream(new File(this.headPath, id + "_s.jpg")), si, true);
347 340
		IoUtil.saveStream(new FileOutputStream(new File(this.headPath, id + "_m.jpg")), mi, true);
348 341
		IoUtil.saveStream(new FileOutputStream(new File(this.headPath, id + "_l.jpg")), li, true);

+ 17 - 2
src/main/java/com/ekexiu/portal/service/ImagesService.java

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

3
import java.awt.image.BufferedImage;
4
import java.io.ByteArrayOutputStream;
3 5
import java.io.File;
4 6
import java.io.FileInputStream;
5 7
import java.io.FileOutputStream;
@ -9,6 +11,8 @@ import java.sql.Connection;
9 11
import java.sql.SQLException;
10 12
import java.util.List;
11 13

14
import javax.imageio.ImageIO;
15

12 16
import org.jfw.util.codec.Base64;
13 17
import org.jfw.apt.annotation.Autowrie;
14 18
import org.jfw.apt.web.annotation.Path;
@ -23,6 +27,7 @@ import org.jfw.util.io.IoUtil;
23 27

24 28
import com.ekexiu.portal.dao.ImageDao;
25 29
import com.ekexiu.portal.po.Image;
30
import com.ekexiu.portal.util.PictureRotating;
26 31

27 32
@Path("/images")
28 33
public class ImagesService {
@ -81,13 +86,23 @@ public class ImagesService {
81 86
	
82 87
	@Post
83 88
	@Path("/saveResImg")
84
	public String insertResourceImg(@JdbcConn(true) Connection con, String resourceId, String base64) throws SQLException, IOException {
89
	public String insertResourceImg(@JdbcConn(true) Connection con, String resourceId, String base64, Integer angle) throws SQLException, IOException {
85 90
		Base64 bs64 = new Base64();
86 91
		byte[] bs = bs64.decode(base64.getBytes("UTF-8"));
87 92
		String imageSrc = this.resourcePath + "/" + resourceId + ".jpg";
88 93
		FileOutputStream fos = new FileOutputStream(imageSrc);
89 94
		try {
90
			fos.write(bs);
95
			if(0 == angle){
96
				fos.write(bs);
97
			}else{
98
				java.awt.Image image = PictureRotating.bytesToImage(bs);
99
				BufferedImage des = PictureRotating.Rotate(image, angle);
100
				ByteArrayOutputStream out = new ByteArrayOutputStream();
101
				@SuppressWarnings("unused")
102
				boolean flag = ImageIO.write(des, "jpg", out);
103
				byte[] bytes = out.toByteArray();
104
				fos.write(bytes);
105
			}
91 106
			fos.flush();
92 107
		} finally {
93 108
			fos.close();

+ 2 - 2
src/main/java/com/ekexiu/portal/service/ResourceService.java

@ -122,11 +122,11 @@ public class ResourceService {
122 122
	
123 123
	@Post
124 124
	@Path("/saveRes")
125
	public String insertRes(@JdbcConn(true) Connection con, Resource resource, @Nullable String base64) {
125
	public String insertRes(@JdbcConn(true) Connection con, Resource resource, @Nullable String base64, Integer angle) {
126 126
		String resourceId = StringUtil.buildUUID();
127 127
		try {
128 128
			if(null != base64){
129
				this.imagesService.insertResourceImg(con, resourceId, base64);
129
				this.imagesService.insertResourceImg(con, resourceId, base64, angle);
130 130
			}
131 131
			resource.setResourceId(resourceId);
132 132
			this.resourceDao.insert(con, resource);