liuxiaolong
2019-05-06 c15226e1b58f255dbebf1bdca8d4e53b9277249c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.basic.analy.utils;
 
import com.cloud.common.model.FileInfos;
import com.cloud.common.utils.FastDFSUtil;
import lombok.Data;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
 
@Log4j
@Component
@Data
public class ImageUtils {
    /**
     * 几种常见的图片格式
     */
    public static String IMAGE_TYPE_GIF = "gif";// 图形交换格式
    public static String IMAGE_TYPE_JPG = "jpg";// 联合照片专家组
    public static String IMAGE_TYPE_JPEG = "jpeg";// 联合照片专家组
    public static String IMAGE_TYPE_BMP = "bmp";// 英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式
    public static String IMAGE_TYPE_PNG = "png";// 可移植网络图形
    public static String IMAGE_TYPE_PSD = "psd";// Photoshop的专用格式Photoshop
 
    @Value("${imgPath.ip}")
    private String  httpImgUrl = "";
 
    @Autowired
    private FastDFSUtil fastDFSUtil;
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
 
    }
    
     /**
     * 图像切割(按指定起点坐标和宽高切割)
     * @param srcImageFile 源图像地址
     * @param result 切片后的图像地址
     * @param x 目标切片起点坐标X
     * @param y 目标切片起点坐标Y
     * @param width 目标切片宽度
     * @param height 目标切片高度
     */
    public final static byte[] cut(String srcImageFile, String result,
            int x, int y, int width, int height) {
        ByteArrayOutputStream outStream=new ByteArrayOutputStream();
        byte[] byteArray=null;
        try {
            // 读取源图像
            BufferedImage bi = ImageIO.read(new File(srcImageFile));
           
            int srcWidth = bi.getWidth(); // 源图宽度
            System.out.println("宽度为"+srcWidth);
            int srcHeight = bi.getHeight(); // 源图高度
            System.out.println("高度为"+srcHeight);
            if (srcWidth > 0 && srcHeight > 0) {
                Image image = bi.getScaledInstance(srcWidth, srcHeight,
                        Image.SCALE_DEFAULT);
                // 四个参数分别为图像起点坐标和宽高
                // 即: CropImageFilter(int x,int y,int width,int height)
                ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
                Image img = Toolkit.getDefaultToolkit().createImage(
                        new FilteredImageSource(image.getSource(),
                                cropFilter));
                BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                Graphics g = tag.getGraphics();
                g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
                g.dispose();
                // 输出为文件
                File file= new File(result);
                if(!file.exists()){
                    file.mkdirs();
                }
                ImageIO.write(tag, "JPEG",file);
                //输出为byte[]数组
                ImageIO.write(tag, "JPEG", outStream);
                byteArray= outStream.toByteArray();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return byteArray;
    }
    
    public static byte[] imageToByte(String path){
        byte[] data = null;
        FileImageInputStream input = null;
        try {
          input = new FileImageInputStream(new File(path));
          ByteArrayOutputStream output = new ByteArrayOutputStream();
          byte[] buf = new byte[1024];
          int numBytesRead = 0;
          while ((numBytesRead = input.read(buf)) != -1) {
          output.write(buf, 0, numBytesRead);
          }
          data = output.toByteArray();
          output.close();
          input.close();
        }
        catch (FileNotFoundException ex1) {
          ex1.printStackTrace();
        }
        catch (IOException ex1) {
          ex1.printStackTrace();
        }
        return data;
    }
    /**
     * 图像切割(按指定起点坐标和宽高切割),切完之后上传图片服务器并返回路径
     * @param fileInfo 源图像地址
     * @param x 目标切片起点坐标X
     * @param y 目标切片起点坐标Y
     * @param width 目标切片宽度
     * @param height 目标切片高度
     */
    public FileInfos cutPhotoFromFast(FileInfos fileInfo,
                                      int x, int y, int width, int height) {
        ByteArrayOutputStream outStream=new ByteArrayOutputStream();
        byte[] byteArray=null;
        FileInfos fileInfo1 = new FileInfos();
        try {
            // 读取源图像
            String path = httpImgUrl+fileInfo.getPath();
            log.info("源图像路径为"+path);
            URL url = new URL(path);
            BufferedImage bi = ImageIO.read(url);
 
            int srcWidth = bi.getWidth(); // 源图宽度
            System.out.println("宽度为"+srcWidth);
            int srcHeight = bi.getHeight(); // 源图高度
            System.out.println("高度为"+srcHeight);
            if (srcWidth > 0 && srcHeight > 0) {
                Image image = bi.getScaledInstance(srcWidth, srcHeight,
                        Image.SCALE_DEFAULT);
                // 四个参数分别为图像起点坐标和宽高
                // 即: CropImageFilter(int x,int y,int width,int height)
                ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
                Image img = Toolkit.getDefaultToolkit().createImage(
                        new FilteredImageSource(image.getSource(),
                                cropFilter));
                BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                Graphics g = tag.getGraphics();
                g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
                g.dispose();
//                // 输出为文件
//                File file= new File(result);
//                if(!file.exists()){
//                    file.mkdirs();
//                }
//                ImageIO.write(tag, "JPEG",file);
                //输出为byte[]数组
                ImageIO.write(tag, "JPEG", outStream);
                byteArray= outStream.toByteArray();
//                fileInfo1 = fastDFSUtil.uploadByByte(byteArray, fileInfo.getExtName());
                FileInfos fileInfos = fastDFSUtil.uploadByByte(byteArray, fileInfo.getExtName());
 
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return fileInfo1;
    }
}