2018년 10월 31일 수요일

[Java] Simple Image Processing (ImageScalr)


ImageScalr Source: https://github.com/rkalla/imgscalr

1. Simple Image Processing
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.FileImageOutputStream;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Method;
import org.imgscalr.Scalr.Mode;
import org.imgscalr.Scalr.Rotation;

import javafx.embed.swing.SwingFXUtils;
import javafx.scene.input.Clipboard;

public class ImageUtil {
    
    //--------------------------------------------------------------------------------
    // BufferedImage
    //--------------------------------------------------------------------------------
 
    public static BufferedImage create(int width, int height) {
        return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);     
    }
    
    public static BufferedImage load(String fileName) throws IOException {
        return ImageIO.read(new File(fileName));
    }
    
    public static void saveToPng(BufferedImage image, String filename) throws IOException {
        ImageIO.write(image, "png", new File(filename));
    }

    public static void saveToJpg(BufferedImage image, String filename) throws IOException {
        ImageIO.write(image, "jpg", new File(filename));
    }

    public static void saveToJpg(BufferedImage image, String filename, float quality) throws IOException {
        JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
        jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        jpegParams.setCompressionQuality(quality);
        final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
        writer.setOutput(new FileImageOutputStream(new File(filename)));
        writer.write(null, new IIOImage(image, null, null), jpegParams);
    }

    public static int getWidth(BufferedImage image) {
        return image.getWidth();
    }

    public static int getHeight(BufferedImage image) {
        return image.getHeight();
    }
    
    public BufferedImage getClipboardImage() {
        return SwingFXUtils.fromFXImage( Clipboard.getSystemClipboard().getImage(), null );
    }
    
    //--------------------------------------------------------------------------------
    // Scalr
    //--------------------------------------------------------------------------------
    public static BufferedImage resize(BufferedImage image, int width, int height) {
        return Scalr.resize( image, Method.ULTRA_QUALITY, Mode.BEST_FIT_BOTH, width, height );
    }
    
    public static BufferedImage resizeExact(BufferedImage image, int width, int height) {
        return Scalr.resize( image, Method.ULTRA_QUALITY, Mode.FIT_EXACT, width, height );
    }
    
    public static BufferedImage resizeWidth(BufferedImage image, int width, int height) {
        return Scalr.resize( image, Method.ULTRA_QUALITY, Mode.FIT_TO_WIDTH, width, height );
    }

    public static BufferedImage resizeHeight(BufferedImage image, int width, int height) {
        return Scalr.resize( image, Method.ULTRA_QUALITY, Mode.FIT_TO_HEIGHT, width, height );
    }   
    
    public static BufferedImage rotate90(BufferedImage image) {
        return Scalr.rotate(image, Rotation.CW_90);
    }

    public static BufferedImage rotate180(BufferedImage image) {
        return Scalr.rotate(image, Rotation.CW_180);
    }
    
    public static BufferedImage rotate270(BufferedImage image) {
        return Scalr.rotate(image, Rotation.CW_270);
    }

    public static BufferedImage rotateFlipHorizontally(BufferedImage image) {
        return Scalr.rotate(image, Rotation.FLIP_HORZ);
    }

    public static BufferedImage rotateFlipVertically(BufferedImage image) {
        return Scalr.rotate(image, Rotation.FLIP_VERT);
    }
    
    public static BufferedImage crop(BufferedImage image, int x, int y, int w, int h) {
        return Scalr.crop(image, x, y, w, h);
    }

    public static BufferedImage pad(BufferedImage image, int pad, Color color) {
        return Scalr.pad(image,  pad,  color);
    }

    //--------------------------------------------------------------------------------
    // Graphics2D
    //--------------------------------------------------------------------------------

    public static BufferedImage mergeHorizontally( BufferedImage[] images, int align ) {
        int width = 0;
        int height = 0;
        for( BufferedImage image : images ) {
            width += image.getWidth();
            if( height < image.getHeight() ) {
                height = image.getHeight();
            }
        }
        BufferedImage merged_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);  
        Graphics gr = merged_image.getGraphics();
        width = 0;
        for( BufferedImage image : images ) {
            switch(align) {
                case -1: gr.drawImage(image, width, 0, null); break;
                case  0: gr.drawImage(image, width, (height-image.getHeight())/2, null); break;
                case  1: gr.drawImage(image, width, height-image.getHeight(), null); break;
            }
            width += image.getWidth();
        }
        gr.dispose();
        return merged_image;
    }

    public static BufferedImage mergeHorizontally( String[] files, int align ) throws IOException {
        BufferedImage[] images = new BufferedImage[files.length];
        for( int i = 0; i < files.length; i++ ) {
            images[i] = ImageIO.read(new File(files[i]));
        }
        return mergeHorizontally( images, align );
    }
    
    public static BufferedImage mergeHorizontally( BufferedImage[] images ) {
        return mergeHorizontally(images, -1);    
    }

    public static BufferedImage mergeHorizontally( String[] files )  throws IOException {
        return mergeHorizontally(files, -1);    
    }
    
    public static BufferedImage mergeVertically( BufferedImage[] images, int align ) {
        int width = 0;
        int height = 0;
        for( BufferedImage image : images ) {
            height += image.getHeight();
            if( width < image.getWidth() ) {
                width = image.getWidth();
            }
        }
        BufferedImage merged_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);  
        Graphics gr = merged_image.getGraphics();
        height = 0;
        for( BufferedImage image : images ) {
            switch(align) {
                case -1: gr.drawImage(image, 0, height, null); break;
                case  0: gr.drawImage(image, (width-image.getWidth())/2, height, null); break;
                case  1: gr.drawImage(image, width-image.getWidth(), height, null); break;
            }
            height += image.getHeight();
        }
        gr.dispose();
        return merged_image;
    }
    
    public static BufferedImage mergeVertically( String[] files, int align ) throws IOException {
        BufferedImage[] images = new BufferedImage[files.length];
        for( int i = 0; i < files.length; i++ ) {
            images[i] = ImageIO.read(new File(files[i]));
        }
        return mergeVertically( images, align );
    }
    
    public static BufferedImage mergeVertically( BufferedImage[] images ) {
        return mergeVertically(images, -1);    
    }

    public static BufferedImage mergeVertically( String[] files )  throws IOException {
        return mergeVertically(files, -1);    
    }
    
    public static void drawLine(BufferedImage image, int x1, int y1, int x2, int y2, Color color) {
        Graphics2D g2 = image.createGraphics();
        g2.setColor(color);
        g2.drawLine(x1,y1,x2,y2);
        g2.dispose();   
    }
    
    public static void drawRect(BufferedImage image, int x, int y, int w, int h, Color color) {
        Graphics2D g2 = image.createGraphics();
        g2.setColor(color);
        g2.drawRect(x,y,w,h);
        g2.dispose();  
    }
    
    public static void drawRoundRect(BufferedImage image, int x, int y, int w, int h, Color color) {
        Graphics2D g2 = image.createGraphics();
        g2.setColor(color);
        g2.drawRoundRect(x,y,w,h,4,4);
        g2.dispose();  
    }
    
    public static void clearRect(BufferedImage image, int x, int y, int w, int h, Color color) {
        Graphics2D g2 = image.createGraphics();
        g2.setBackground(color);
        g2.drawRect(x,y,w,h);
        g2.dispose();   
    }
    
    public static void fillRect(BufferedImage image, int x, int y, int w, int h, Color color) {
        Graphics2D g2 = image.createGraphics();
        g2.setColor(color);
        g2.fillRect(x,y,w,h);
        g2.dispose();   
    }
    
    public static void drawText(BufferedImage image, String text, int x, int y, Color color) {
        Graphics2D g2 = image.createGraphics();
        g2.setColor(color);
        g2.drawString(text,x,y);
        g2.dispose(); 
    }
    
    public static void drawImage(BufferedImage image, int x, int y) {
        Graphics2D g2 = image.createGraphics();
        g2.drawImage(image,x,y,null);
        g2.dispose();
    }
    
    public static void showImage(BufferedImage image, boolean undecorated) {
        JFrame frame = new JFrame();
        ImageIcon icon = new ImageIcon(image);
        JLabel label = new JLabel(icon);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        if(undecorated) {
            frame.setUndecorated(true); //removes the surrounding border
        }
        frame.getContentPane().add(label); 
        frame.setSize(icon.getIconWidth(), icon.getIconHeight()); 
        int x = (screenSize.width - frame.getSize().width)/2;
        int y = (screenSize.height - frame.getSize().height)/2;
        frame.setLocation(x, y); 
        frame.pack();
        frame.setVisible(true); 
    }
    
    public static void showImage(BufferedImage image) {
        showImage( image, false );
    }
}


댓글 없음:

댓글 쓰기