Browse Source

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

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

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

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

2

3
import java.awt.image.BufferedImage;
4
import java.io.ByteArrayOutputStream;
3
import java.io.File;
5
import java.io.File;
4
import java.io.FileInputStream;
6
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
7
import java.io.FileOutputStream;
9
import java.sql.SQLException;
11
import java.sql.SQLException;
10
import java.util.List;
12
import java.util.List;
11

13

14
import javax.imageio.ImageIO;
15

12
import org.jfw.util.codec.Base64;
16
import org.jfw.util.codec.Base64;
13
import org.jfw.apt.annotation.Autowrie;
17
import org.jfw.apt.annotation.Autowrie;
14
import org.jfw.apt.web.annotation.Path;
18
import org.jfw.apt.web.annotation.Path;
23

27

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

31

27
@Path("/images")
32
@Path("/images")
28
public class ImagesService {
33
public class ImagesService {
81
	
86
	
82
	@Post
87
	@Post
83
	@Path("/saveResImg")
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
		Base64 bs64 = new Base64();
90
		Base64 bs64 = new Base64();
86
		byte[] bs = bs64.decode(base64.getBytes("UTF-8"));
91
		byte[] bs = bs64.decode(base64.getBytes("UTF-8"));
87
		String imageSrc = this.resourcePath + "/" + resourceId + ".jpg";
92
		String imageSrc = this.resourcePath + "/" + resourceId + ".jpg";
88
		FileOutputStream fos = new FileOutputStream(imageSrc);
93
		FileOutputStream fos = new FileOutputStream(imageSrc);
89
		try {
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
			fos.flush();
106
			fos.flush();
92
		} finally {
107
		} finally {
93
			fos.close();
108
			fos.close();

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

122
	
122
	
123
	@Post
123
	@Post
124
	@Path("/saveRes")
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
		String resourceId = StringUtil.buildUUID();
126
		String resourceId = StringUtil.buildUUID();
127
		try {
127
		try {
128
			if(null != base64){
128
			if(null != base64){
129
				this.imagesService.insertResourceImg(con, resourceId, base64);
129
				this.imagesService.insertResourceImg(con, resourceId, base64, angle);
130
			}
130
			}
131
			resource.setResourceId(resourceId);
131
			resource.setResourceId(resourceId);
132
			this.resourceDao.insert(con, resource);
132
			this.resourceDao.insert(con, resource);