Browse Source

图片旋转处理保存。

zzy.zhiyuan.foxmail 8 years ago
parent
commit
55c4cfea6e

+ 25 - 0
src/main/java/com/ekexiu/portal/service/ImageService.java

@ -1,5 +1,7 @@
1 1
package com.ekexiu.portal.service;
2 2
3
import java.awt.Image;
4
import java.awt.image.BufferedImage;
3 5
import java.io.ByteArrayInputStream;
4 6
import java.io.ByteArrayOutputStream;
5 7
import java.io.File;
@ -10,6 +12,9 @@ import java.io.InputStream;
10 12
import java.sql.SQLException;
11 13
import java.util.concurrent.TimeUnit;
12 14
15
import javax.imageio.ImageIO;
16
17
import org.jfw.apt.annotation.Nullable;
13 18
import org.jfw.apt.web.annotation.Path;
14 19
import org.jfw.apt.web.annotation.operate.Get;
15 20
import org.jfw.apt.web.annotation.operate.Post;
@ -20,6 +25,8 @@ import org.jfw.util.context.JfwAppContext;
20 25
import org.jfw.util.exception.JfwBaseException;
21 26
import org.jfw.util.io.IoUtil;
22 27
28
import com.ekexiu.portal.util.PictureRotating;
29
23 30
@Path("/image")
24 31
public class ImageService {
25 32
	private File tmpPath;
@ -323,5 +330,23 @@ public class ImageService {
323 330
		IoUtil.saveStream(new FileOutputStream(new File(this.headPath, id + "_l.jpg")), li, true);
324 331
		return true;
325 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);
346
		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);
348
		IoUtil.saveStream(new FileOutputStream(new File(this.headPath, id + "_l.jpg")), li, true);
349
		return true;
350
	}
326 351
327 352
}

+ 70 - 0
src/main/java/com/ekexiu/portal/util/PictureRotating.java

@ -0,0 +1,70 @@
1
package com.ekexiu.portal.util;
2

3
import java.awt.Dimension;
4
import java.awt.Graphics2D;
5
import java.awt.Image;
6
import java.awt.Label;
7
import java.awt.MediaTracker;
8
import java.awt.Rectangle;
9
import java.awt.Toolkit;
10
import java.awt.image.BufferedImage;
11

12
public class PictureRotating {
13
	public static BufferedImage Rotate(Image src, int angel) {  
14
        int src_width = src.getWidth(null);  
15
        int src_height = src.getHeight(null);  
16
        // calculate the new image size  
17
        Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(  
18
                src_width, src_height)), angel);  
19
  
20
        BufferedImage res = null;  
21
        res = new BufferedImage(rect_des.width, rect_des.height,  
22
                BufferedImage.TYPE_INT_RGB);  
23
        Graphics2D g2 = res.createGraphics();  
24
        // transform  
25
        g2.translate((rect_des.width - src_width) / 2,  
26
                (rect_des.height - src_height) / 2);  
27
        g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);  
28
  
29
        g2.drawImage(src, null, null);  
30
        return res;  
31
    }  
32
  
33
    public static Rectangle CalcRotatedSize(Rectangle src, int angel) {  
34
        // if angel is greater than 90 degree, we need to do some conversion  
35
        if (angel >= 90) {  
36
            if(angel / 90 % 2 == 1){  
37
                int temp = src.height;  
38
                src.height = src.width;  
39
                src.width = temp;  
40
            }  
41
            angel = angel % 90;  
42
        }  
43
  
44
        double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;  
45
        double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;  
46
        double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;  
47
        double angel_dalta_width = Math.atan((double) src.height / src.width);  
48
        double angel_dalta_height = Math.atan((double) src.width / src.height);  
49
  
50
        int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha  
51
                - angel_dalta_width));  
52
        int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha  
53
                - angel_dalta_height));  
54
        int des_width = src.width + len_dalta_width * 2;  
55
        int des_height = src.height + len_dalta_height * 2;  
56
        return new Rectangle(new Dimension(des_width, des_height));  
57
    }
58
    
59
    public static Image bytesToImage(byte[] bytes) {
60
    	Image image = Toolkit.getDefaultToolkit().createImage(bytes);
61
    	try {
62
    	MediaTracker mt = new MediaTracker(new Label());
63
    	mt.addImage(image, 0);
64
    	mt.waitForAll();
65
    	} catch (InterruptedException e) {
66
    	e.printStackTrace();
67
    	}
68
    	return image;
69
    }
70
}