<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 閱讀(4420) 評論(0)  編輯  收藏 所屬分類: Graphics

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

    常用鏈接

    留言簿(1)

    隨筆分類(13)

    隨筆檔案(13)

    文章分類(4)

    文章檔案(5)

    相冊

    Developer

    Favorite blogs

    搜索

    •  

    積分與排名

    • 積分 - 22403
    • 排名 - 1627

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费一级毛片无毒不卡| 亚洲欧洲无码AV电影在线观看| 丝袜捆绑调教视频免费区| 亚洲AV成人噜噜无码网站| 亚洲产国偷V产偷V自拍色戒| 免费a级毛片无码av| 免费做爰猛烈吃奶摸视频在线观看| 免费看一区二区三区四区| 人妻仑刮八A级毛片免费看| 亚洲码和欧洲码一码二码三码 | 免费在线观看一区| 亚洲最大的黄色网| 亚洲高清在线mv| 久久亚洲一区二区| 国产亚洲精品美女久久久| 精品国产亚洲男女在线线电影 | 亚洲伊人久久综合影院| 国产免费一区二区三区VR| 成年性午夜免费视频网站不卡| 99久久免费中文字幕精品| 国内精品免费在线观看| yy一级毛片免费视频| 国产亚洲午夜精品| 亚洲成a人无码亚洲成av无码| 丁香婷婷亚洲六月综合色| 亚洲国产成人久久三区| 亚洲日产2021三区在线 | 久久国产精品2020免费m3u8| 本免费AV无码专区一区| 国产精品免费αv视频| 一级视频免费观看| 亚洲视频在线免费| 在线观看免费无码视频| 国产午夜成人免费看片无遮挡 | 亚洲国产精品成人久久蜜臀| 亚洲?V无码成人精品区日韩| 亚洲国产成人久久综合野外| 亚洲精品国产日韩无码AV永久免费网| 亚洲av午夜精品一区二区三区| 亚洲国产高清精品线久久| 亚洲色精品aⅴ一区区三区|