<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Pudgy's World
    posts - 13,  comments - 16,  trackbacks - 0

    From http://schmidt.devlib.org/java/save-jpeg-thumbnail.html

    Thumbnail.java - Load an image, scale it to thumbnail size and save it as JPEG

    This programm loads an image via java.awt.Toolkit, scales it down to a user-defined resolution and saves it as a JPEG file. The first part, the loading of the original image, is done the same way as in Viewer. So if you don't know yet how loading images with Toolkit works you might want to study that program first.

    Different from Viewer, this program (Thumbnail) works on the command line. So you won't get any windows or other graphical user interface components. The only visual feedback is the word Done. after the program successfully terminated.

    To use this program do the following:

    • Save the program's source code as Thumbnail.java (regard case).
    • Open a shell (prompt), go to the directory where Thumbnail.java is and compile it:
      javac Thumbnail.java
      You should now have a new class file Thumbnail.class.
    • Run the program with five parameters for image file, thumbnail file, thumbnail width and thumbnail height and quality (a value from 0 to 100, 100 being the best and 0 the worst quality), e.g.:
      java Thumbnail c:\image.jpg c:\thumbnail.jpg 120 80 75
      The file image.jpg must exist already, thumbnail.jpg will be created (and any existing file of that name overwritten).

    You will need Java 1.2 or higher to successfully run this program. The com.sun.image.codec.jpeg package that will be used for saving the thumbnail is not available with all Java development kits, but as long as you are using a Sun JDK, it should be present.

    With Java 1.4 a new way of writing JPEG files was introduced, the image I/O library in the package javax.imageio. See the Screenshot.java example program. It saves as PNG, but all you have to do is change the second argument of ImageIO.write from png to jpg. The advantage of ImageIO: It is available with each 1.4+ JDK and JRE, not only those coming from Sun.

    Explanation

    Now let's see how this program works. First, it is checked that we have exactly five arguments. If this is not the case, an error message is printed to output and the program terminates.

    Next, the input image is loaded via Toolkit and MediaTracker just as it was done in Viewer.

    The third and fourth program argument contain the maximum size of the thumbnail to be created. The actual size of the thumbnail will be computed from that maximum size and the actual size of the image (all sizes are given as pixels). The code that does this is not really very readable, and also not essential to loading and saving image files. But it is necessary to create a thumbnail that is scaled correctly.

    As an example, if the two arguments for the maximum thumbnail size are both 100 and the image that was loaded is 400 times 200 pixels large, we want the thumbnail to be 100 times 50 pixels large, not 100 times 100, because the original image is twice as wide as it is high. A 100 times 100 pixel thumbnail would contain a very skewed version of the original image.

    Now that we have determined the size of the thumbnail we create a BufferedImage of that size, named thumbImage. We ask for a Graphics2D object for that new thumbnail image and call its drawImage method to draw the original image on that new image. The call to drawImage does the actual scaling. The rendering hints for bilinear interpolation can be left out if quality is not that necessary and speed more important. For nicer results (at least in some cases) try RenderingHints.VALUE_INTERPOLATION_BICUBIC instead of RenderingHints.VALUE_INTERPOLATION_BILINEAR.

    In order to save the scaled-down image to a JPEG file, we create a buffered FileOutputStream with the second argument as name and initialize the necessary objects from the com.sun.image.codec.jpeg package. The quality argument from the command line is converted from the interval 0 to 100 to the interval 0.0f to 1.0f, because that's what the codec expects (I mostly use 0.75f). The higher that quality number is, the better the resulting thumbnail image quality, but also the larger the resulting file.

    The call to System.exit(0); is unfortunately necessary for some Java runtime environments (because of a bug that keeps the AWT thread from terminating).

    Source code of Thumbnail.java

     
    import com.sun.image.codec.jpeg.*;
    import java.awt.
    *
    ;
    import java.awt.image.
    *
    ;
    import java.io.
    *
    ;

    /*
    *
     * Thumbnail.java (requires Java 1.2+)
     * Load an image, scale it down and save it as a JPEG file.
     * @author Marco Schmidt
     
    */

    public class Thumbnail {
      
    public static void
     main(String[] args) throws Exception {
        
    if (args.length != 5
    ) {
          System.err.println(
    "Usage: java Thumbnail INFILE " +

            
    "OUTFILE WIDTH HEIGHT QUALITY");
          System.exit(
    1
    );
        }
        
    // load image from INFILE

        Image image = Toolkit.getDefaultToolkit().getImage(args[0]);
        MediaTracker mediaTracker 
    = new MediaTracker(new
     Container());
        mediaTracker.addImage(image, 
    0
    );
        mediaTracker.waitForID(
    0
    );
        
    // determine thumbnail size from WIDTH and HEIGHT

        int thumbWidth = Integer.parseInt(args[2]);
        
    int thumbHeight = Integer.parseInt(args[3
    ]);
        
    double thumbRatio = (double)thumbWidth / (double
    )thumbHeight;
        
    int imageWidth = image.getWidth(null
    );
        
    int imageHeight = image.getHeight(null
    );
        
    double imageRatio = (double)imageWidth / (double
    )imageHeight;
        
    if (thumbRatio <
     imageRatio) {
          thumbHeight 
    = (int)(thumbWidth /
     imageRatio);
        } 
    else
     {
          thumbWidth 
    = (int)(thumbHeight *
     imageRatio);
        }
        
    //
     draw original image to thumbnail image object and
        
    // scale it to the new size on-the-fly

        BufferedImage thumbImage = new BufferedImage(thumbWidth, 
          thumbHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D 
    =
     thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
          RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 
    00, thumbWidth, thumbHeight, null
    );
        
    // save thumbnail image to OUTFILE

        BufferedOutputStream out = new BufferedOutputStream(new
          FileOutputStream(args[
    1]));
        JPEGImageEncoder encoder 
    = JPEGCodec.createJPEGEncoder(out
    );
        JPEGEncodeParam param 
    =
     encoder.
          getDefaultJPEGEncodeParam(thumbImage);
        
    int quality = Integer.parseInt(args[4
    ]);
        quality 
    = Math.max(0, Math.min(quality, 100
    ));
        param.setQuality((
    float)quality / 100.0ffalse
    );
        encoder.setJPEGEncodeParam(param);
        encoder.encode(thumbImage);
        
    out
    .close(); 
        System.
    out.println("Done."
    );
        System.exit(
    0
    );
      }
    }
    posted on 2005-08-29 16:12 Pudgy's World 閱讀(4430) 評論(0)  編輯  收藏 所屬分類: Graphics

    <2005年8月>
    31123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    常用鏈接

    留言簿(1)

    隨筆分類(13)

    隨筆檔案(13)

    文章分類(4)

    文章檔案(5)

    相冊

    Developer

    Favorite blogs

    搜索

    •  

    積分與排名

    • 積分 - 22586
    • 排名 - 1626

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久精品国产亚洲AV网站| 91精品成人免费国产片| 色偷偷亚洲第一综合网| 亚洲av成人一区二区三区在线播放| 日本精品久久久久久久久免费| 国产黄片不卡免费| 131美女爱做免费毛片| 日本19禁啪啪无遮挡免费动图| 亚洲精品A在线观看| 亚洲AV无码一区二区三区系列| a毛片在线看片免费| 免费在线看v网址| 亚洲区小说区图片区| xxxxxx日本处大片免费看| 最近的中文字幕大全免费版| 男人的天堂亚洲一区二区三区 | 成人片黄网站色大片免费| 亚洲另类古典武侠| 精品一区二区三区无码免费直播 | 日韩免费高清播放器| 日本免费高清一本视频| 国产精品久久久久久亚洲小说| 亚洲Av无码乱码在线znlu| 亚洲国产综合第一精品小说| 无码 免费 国产在线观看91| 岛国大片免费在线观看| 亚洲综合一区二区精品久久| 一级特黄录像免费播放肥| 亚洲AV无码乱码在线观看性色扶 | 亚洲精品国产精品国自产网站 | 亚洲妇女水蜜桃av网网站| 毛片网站免费在线观看| 67pao强力打造67194在线午夜亚洲 | 美女18毛片免费视频| 亚洲综合AV在线在线播放| 亚洲aⅴ无码专区在线观看春色 | 国产一级大片免费看| 亚洲午夜精品国产电影在线观看| 成人免费无码大片A毛片抽搐 | 国产亚洲色视频在线| 深夜特黄a级毛片免费播放|