<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

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲综合校园春色| 免费国产高清视频| 你是我的城池营垒免费观看完整版| 91亚洲精品麻豆| 亚洲AV综合色一区二区三区| 女人张开腿等男人桶免费视频| 在线成人爽a毛片免费软件| 最新久久免费视频| 久久久WWW成人免费精品| 久久久久亚洲国产AV麻豆| 国产亚洲精品bv在线观看| 久久亚洲精品无码aⅴ大香| 亚洲精品乱码久久久久久中文字幕 | 99久久99这里只有免费的精品| 美女扒开尿口给男人爽免费视频| 亚洲kkk4444在线观看| 亚洲av极品无码专区在线观看| 久久久无码精品亚洲日韩蜜臀浪潮| 精品亚洲综合在线第一区| 亚洲中文字幕无码中文字在线| 国产日产亚洲系列最新| 亚洲日韩在线观看免费视频| mm1313亚洲精品国产| 亚洲国产黄在线观看| 亚洲精品国产日韩无码AV永久免费网 | 女人18毛片特级一级免费视频 | 亚洲午夜在线一区| 911精品国产亚洲日本美国韩国| 亚洲va无码va在线va天堂| 亚洲AV综合色一区二区三区| 亚洲av日韩av不卡在线观看| 亚洲Av熟妇高潮30p| 亚洲av无码成人黄网站在线观看| 亚洲成AV人片在线观看无码 | 国产伦精品一区二区免费| eeuss影院免费直达入口| 国产精品1024在线永久免费| 三级黄色在线免费观看| 一个人看的www免费视频在线观看 一个人免费视频观看在线www | 无码国产精品一区二区免费vr| 99热这里只有精品免费播放|