JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區 » Image  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 [圖像] Basic Image Thumbnail Generator
T55555

Java, Ruby, Haskell

版主

發文: 1026
積分: 24
於 2004-02-28 06:48 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
轉我以前的貼 ...

Here I will show you how easily create your own image thumbnail by using Java.

Write an image thumbnail generator??
I known, I known, there were a lot of (free) tools can create image thumbnail.
But, by creating this kind of tool, not only allow you learn more Java ( image io package ), you will have total control of your application too.
(Not only scale the image, you could rotate or add more filters, ... etc)

JDK 1.4 added image io package, this help us a lot.
The main idea is
(1) Using image io to real the image file to buffered image.
(2) Using Graphics(2D) drawImage method to scale image.
(3) Using image io to write the thumbnail image.

The getScaleImage method, will create a new buffered image, then create the graphics device from that image, and draw the scaled image on it. By this way, I will get the thumbnail image.

The scaleImage method will take 2 File object, one for input, the other for output, by using image io's ImageIO class to read the origin image file and write the thumbnail image to output file.

Note: the ImageIO.read return null if no image reader correspond image format, and ImageIO.write return false if not image writer correspond the image format.

Here is the simple ImageUtilities class:
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
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
 
/**
 * Image utilities class.
 *
 * @author T55555
 * @version 1.0 2003-01-29
 */
public class ImageUtilities {
 
    /**
     * Return scaled image.
     * Pre-conditions: (source != null) && (width > 0) && (height > 0)
     *
     * @param source the image source
     * @param width the new image's width
     * @param height the new image's height
     * @return the new image scaled
     */
    public static BufferedImage getScaleImage(BufferedImage source,
                                              int width, int height) {
        //assert(source != null && width > 0 && height > 0);
        BufferedImage image = new BufferedImage(width, height, source.getType());
        image.createGraphics().drawImage(source, 0, 0, width, height, null);
        return image;
    }
 
    /**
     * Return scaled image.
     * Pre-conditions: (source != null) && (xscale > 0) && (yscale > 0)
     *
     * @param source the image source
     * @param xscale the percentage of the source image's width
     * @param yscale the percentage of the source image's height
     * @return the new image scaled
     */
    public static BufferedImage getScaleImage(BufferedImage source,
                                              double xscale, double yscale) {
        //assert(source != null && width > 0 && height > 0);
        return getScaleImage(source,
                (int)(source.getWidth() * xscale), (int)(source.getHeight() * yscale));
    }
 
    /**
     * Read the input image file, scaled then write the output image file.
     *
     * @param input the input image file
     * @param output the output image file
     * @param width the output image's width
     * @param height the output image's height
     * @return true for sucessful,
     * false if no appropriate reader or writer is found.
     */
    public static boolean scaleImage(File input, File output,
                                     int width, int height) throws IOException {
        BufferedImage image = ImageIO.read(input);
        if (image == null) { return false; }
        image = getScaleImage(image, width, height);
        String name = output.getName();
        String format = name.substring(name.lastIndexOf('.')+1).toLowerCase();
        return ImageIO.write(image, format, output);
    }
  
    /**
     * Read the input image file, scaled then write the output image file.
     *
     * @param input the input image file
     * @param output the output image file
     * @param xscale the percentage of the input image's width for output image's width
     * @param yscale the percentage of the input image's height for output image's height
     * @return true for sucessful,
     * false if no appropriate reader or writer is found.
     */
    public static boolean scaleImage(File input, File output,
                                     double xscale, double yscale) throws IOException {
        BufferedImage image = ImageIO.read(input);
        if (image == null) { return false; }
        image = getScaleImage(image, xscale, yscale);
        String name = output.getName();
        String format = name.substring(name.lastIndexOf('.')+1).toLowerCase();
        return ImageIO.write(image, format, output);
    }
}


There are many way to use this IamgeUtilities class, I like create a
command-line application, here is the Thumbnail program:

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
import java.io.*; 
import java.util.*; 
  
public class Thumbnail { 
    static void printHelp() { 
        System.out.println( 
                "Systax: java Thumbnail -w width -h height -o outdir -i file [file] ..." + 
                "\n\t width ..... : new width or percentage scale" + 
                "\n\t height .... : new height or percentage scale" + 
                "\n\t outdir .... : output directory" + 
                "\n\t file(s) ... : image source file(s)" + 
                "\n\nExample: java Thumbnail -w .25 -h .25 -o c:\\out -i c:\\in\\*.jpg" ); 
        System.exit(0); 
    } 
      
    public static void main(String[] args) throws IOException { 
        if (args.length < 8) { 
            printHelp(); 
        } 
         
        boolean iFlag = false; 
        boolean wFlag = false; 
        boolean hFlag = false; 
        boolean oFlag = false; 
         
        Vector files = new Vector(); 
        File outDir = null; 
        double xscale = -1.0; 
        double yscale = -1.0; 
        int width = -1; 
        int height = -1; 
         
        for (int i = 0; i < args.length; ) { 
            String arg = args[i++]; 
            if (iFlag) { 
                files.add(new File(arg));                 
            } else if (arg.equalsIgnoreCase("-i")) { 
                iFlag = true; 
                if (!(oFlag && wFlag && hFlag)) { 
                    printHelp(); 
                }                 
            } else if (!oFlag && arg.equalsIgnoreCase("-o")) { 
                oFlag = true; 
                outDir = new File(args[i++]);                 
            } else if (!wFlag && arg.equalsIgnoreCase("-w")) { 
                wFlag = true; 
                arg = args[i++]; 
                try { 
                    width = Integer.parseInt(arg); 
                } catch (NumberFormatException e) { 
                    try { 
                        xscale = Double.parseDouble(arg); 
                    } catch (NumberFormatException ee) { 
                        printHelp(); 
                    } 
                }                 
            } else if (!hFlag && arg.equalsIgnoreCase("-h")) { 
                hFlag = true; 
                arg = args[i++]; 
                try { 
                    height = Integer.parseInt(arg); 
                } catch (NumberFormatException e) { 
                    try { 
                        yscale = Double.parseDouble(arg); 
                    } catch (NumberFormatException ee) { 
                        printHelp(); 
                    } 
                }                 
            } else { 
                printHelp(); 
            } 
        } 
        
        //@todo: more test about xscale, yscale, width, height ... 
        if ((xscale <= 0) && (yscale <= 0) && (width <=0) && (height <= 0)) { 
            printHelp(); 
        } 
        
        boolean useInteger = (xscale < 0) && (yscale < 0); 
        
        for (Iterator i = files.iterator(); i.hasNext(); ) { 
            File input = (File)i.next(); 
            File output = new File(outDir, input.getName()+".png"); 
            ImageUtilities.scaleImage(input, output, 
                    useInteger ? width : xscale, useInteger ? height : yscale); 
        } 
        
        System.out.println("Sucessful, total " + files.size() + " thumbnail(s) generated."); 
    } 
} 

I really use this tool to create my jpg images thumbnail.

Notes:
(1) The Thumbnail program has more "to do" things.
(2) This tool will not create GIF thumbnail. ( no default image writer support )
(3) The default image io package only read jpg, png, gif format,
and only write to jpg and png format.
( No gif format wirter support, we all know why )
(4) You could create a GUI application. ( exercise, it is not difficult at all )
(5) By using image io package, you could transfer one image format to others.
( exercise, image transformer application ? )
(6) By using JAI ( java advance image), you could have more image format support like bmp, tiff.
(7) The ImageUtilities may have trouble with "multi-image" format file.
(8) You could add any image format plug-in you like to support gif, ico, … etc format.
( How to write plug-in, that will be an other topic ).


caterpillar edited on 2004-07-16 13:14
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
11907 [圖像] Basic Image Thumbnail Generator T55555 9224 2004-02-28 06:48
5947 Re:[分享] Java - Basic Image Thumbnail Generator T55555 348 2004-02-28 06:51
» JWorld@TW »  Java 程式分享區 » Image

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8