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

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

    常用鏈接

    留言簿(1)

    隨筆分類(13)

    隨筆檔案(13)

    文章分類(4)

    文章檔案(5)

    相冊

    Developer

    Favorite blogs

    搜索

    •  

    積分與排名

    • 積分 - 22439
    • 排名 - 1627

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 蜜臀亚洲AV无码精品国产午夜.| 亚洲精品国产精品乱码视色| 中文字幕亚洲精品| 久久国产精品国产自线拍免费| 亚洲精品老司机在线观看| 国产亚洲精品仙踪林在线播放| 最新仑乱免费视频| 亚洲GV天堂GV无码男同| 国产日产成人免费视频在线观看| 午夜亚洲WWW湿好爽| 日韩成人免费aa在线看| 国产精品自拍亚洲| 亚洲欧洲国产成人综合在线观看| 一级中文字幕免费乱码专区 | 亚洲精品午夜无码电影网| h视频免费高清在线观看| 亚洲一区二区三区在线观看精品中文| CAOPORN国产精品免费视频| 亚洲AV永久精品爱情岛论坛| 日韩精品极品视频在线观看免费| 亚洲最大免费视频网| 成人特黄a级毛片免费视频| 日韩色视频一区二区三区亚洲 | 国产成人福利免费视频| 国产成人精品日本亚洲专区6| 免费黄色小视频网站| 一级做a免费视频观看网站| 亚洲精品你懂的在线观看| 真人做A免费观看| 无码亚洲成a人在线观看| 亚洲人JIZZ日本人| 岛国av无码免费无禁网站| 美女隐私免费视频看| 亚洲AV日韩精品久久久久久久| 可以免费看黄视频的网站| 国产综合激情在线亚洲第一页| 亚洲国产精品久久久久| 夫妻免费无码V看片| 91视频免费网站| 亚洲人成电影网站免费| 亚洲无线码在线一区观看|