·ÖÀà

  • Èí¼þÌìµØ

  • printscreen¶ÁȡλͼתΪjpeg¸ñʽÊä³ö¡¬
    ¡¡¡¡import java.awt.*;                                          
    ¡¡¡¡import java.awt.image.BufferedImage;      
    ¡¡¡¡import java.io.*;                                            
    ¡¡¡¡import java.util.*;                                        
    ¡¡¡¡import com.sun.image.codec.jpeg.*;          
    ¡¡¡¡import java.awt.Image;                                  
    ¡¡¡¡import java.awt.Toolkit;                              
    ¡¡¡¡import java.awt.image.BufferedImage;      

    ¡¡¡¡import java.awt.imag
    e.MemoryImageSource;
    ¡¡¡¡import java.io.FileInputStream;                
    ¡¡¡¡import java.io.FileOutputStream;              

    ¡¡¡¡import com.sun.image
    .codec.jpeg.JPEGCodec;
    ¡¡¡¡import com.sun.image
    .codec.jpeg.JPEGImageEncoder
    ;

    ¡¡¡¡public class ScreenPicSaver                        
    ¡¡¡¡{                                                                            
    ¡¡¡¡¡¡MediaTracker tracker = new Med
    iaTracker(new Component()
    ¡¡¡¡¡¡{});                                                                    

    ¡¡¡¡¡¡public ScreenPicSaver()                              
    ¡¡¡¡¡¡{}                                                                        

    ¡¡¡¡¡¡public Image loadbitmap(String
    sdir, String sfile) //¶ÁÈ¡8λ»ò24λµÄλͼÎļþ
    ¡¡¡¡¡¡{                                                                          
    ¡¡¡¡¡¡¡¡Image image;                                                  
    ¡¡¡¡¡¡¡¡System.out.print
    ln("loading:" + sdir + sfile
    );
    ¡¡¡¡¡¡¡¡try                                                                    
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡FileInputStream fs = new F
    ileInputStream(sdir + sfile);
    ¡¡¡¡¡¡¡¡¡¡int bflen = 14
    ; // 14 ×Ö½Ú BITMAPFILEHEADE
    R
    ¡¡¡¡¡¡¡¡¡¡byte bf[] = new byte[bflen];                
    ¡¡¡¡¡¡¡¡¡¡fs.read(bf, 0, bflen);                            
    ¡¡¡¡¡¡¡¡¡¡int bilen = 40
    ; // 40 ×Ö½Ú BITMAPINFOHEADE
    R
    ¡¡¡¡¡¡¡¡¡¡byte bi[] = new byte[bilen];                
    ¡¡¡¡¡¡¡¡¡¡fs.read(bi, 0, bilen);                            

    ¡¡¡¡¡¡¡¡¡¡// ½âÊÍÊý¾Ý¡£                                                        
    ¡¡¡¡¡¡¡¡¡¡int nsize = (
    ( (int) bf[5] & 0xff) < &
    lt;¡¡24)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) bf[4] & 0x
    ff) < <¡¡16)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) bf[3] & 0x
    ff) < <¡¡8)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) bf[2] & 0xff;                          
    ¡¡¡¡¡¡¡¡¡¡System.out.pri
    ntln("File type is :" + (cha
    r) bf[0] + (char) bf[1]);
    ¡¡¡¡¡¡¡¡¡¡System.out.pri
    ntln("Size of file is :" + n
    size);

    ¡¡¡¡¡¡¡¡¡¡int nbisize = ( ( (int) bi
    [3] & 0xff) < <¡¡24)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int
    ) bi[2] & 0xff) < <¡¡1
    6)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int
    ) bi[1] & 0xff) < <¡¡8
    )
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) bi[0] & 0xff;                          
    ¡¡¡¡¡¡¡¡¡¡System.out.println("Size o
    f bitmapinfoheader is :" + nbisize);

    ¡¡¡¡¡¡¡¡¡¡int nwidth = (
    ( (int) bi[7] & 0xff) <
    <¡¡24)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) bi[6] & 0x
    ff) < <¡¡16)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int
    ) bi[5] & 0xff) < <¡¡8
    )
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) bi[4] & 0xff;                          

    ¡¡¡¡¡¡¡¡¡¡System.out.pri
    ntln("Width is :" + nwidth);

    ¡¡¡¡¡¡¡¡¡¡int nheight = ( ( (int) bi
    [11] & 0xff) < <¡¡24)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int
    ) bi[10] & 0xff) < <¡¡
    16)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) bi[9] & 0x
    ff) < <¡¡8)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) bi[8] & 0xff;                          
    ¡¡¡¡¡¡¡¡¡¡System.out.println("Height
    is :" + nheight);

    ¡¡¡¡¡¡¡¡¡¡int nplanes =
    0xff;
    ( ( (int) bi[13] & 0xff) <

    ; <¡¡8) | (int) bi[12] &

    ¡¡¡¡¡¡¡¡¡¡System.out.pri
    ntln("Planes is :" + nplanes
    );

    ¡¡¡¡¡¡¡¡¡¡int nbitcount = ( ( (int)
    0xff;
    bi[15] & 0xff) < <¡¡8) | (int) bi[14] &

    ¡¡¡¡¡¡¡¡¡¡System.out.println("BitCou
    nt is :" + nbitcount);

    ¡¡¡¡¡¡¡¡¡¡// ²éÕÒ±íÃ÷ѹËõµÄ·ÇÁãÖµ                                              
    ¡¡¡¡¡¡¡¡¡¡int ncompressi
    on = ( ( (int) bi[19]) <
    <¡¡24)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) bi[18]) < <¡¡16)  
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) bi[17]) < <¡¡8)    
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) bi[16];                                      
    ¡¡¡¡¡¡¡¡¡¡System.out.pri
    ntln("Compression is :" + nc
    ompression);

    ¡¡¡¡¡¡¡¡¡¡int nsizeimage
    = ( ( (int) bi[23] & 0xff)
    < <¡¡24)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int
    ) bi[22] & 0xff) < <¡¡
    16)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int
    ) bi[21] & 0xff) < <¡¡
    8)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) bi[20] & 0xff;                        
    ¡¡¡¡¡¡¡¡¡¡System.out.println("SizeIm
    age is :" + nsizeimage);

    ¡¡¡¡¡¡¡¡¡¡int nxpm = ( (
    (int) bi[27] & 0xff) < &
    lt;¡¡24)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int
    ) bi[26] & 0xff) < <¡¡
    16)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) bi[25] & 0
    xff) < <¡¡8)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) bi[24] & 0xff;                        
    ¡¡¡¡¡¡¡¡¡¡System.out.pri
    ntln("X-Pixels per meter is
    :" + nxpm);

    ¡¡¡¡¡¡¡¡¡¡int nypm = ( ( (int) bi[31
    ] & 0xff) < <¡¡24)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int
    ) bi[30] & 0xff) < <¡¡
    16)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int
    ) bi[29] & 0xff) < <¡¡
    8)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) bi[28] & 0xff;                        
    ¡¡¡¡¡¡¡¡¡¡System.out.println("Y-Pixe
    ls per meter is :" + nypm);

    ¡¡¡¡¡¡¡¡¡¡int nclrused =
    ( ( (int) bi[35] & 0xff) &l
    t; <¡¡24)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) bi[34] & 0
    xff) < <¡¡16)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) bi[33] & 0
    xff) < <¡¡8)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) bi[32] & 0xff;                        
    ¡¡¡¡¡¡¡¡¡¡System.out.pri
    ntln("Colors used are :" + n
    clrused);

    ¡¡¡¡¡¡¡¡¡¡int nclrimp = ( ( (int) bi
    [39] & 0xff) < <¡¡24)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) bi[38] & 0
    xff) < <¡¡16)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) bi[37] & 0
    xff) < <¡¡8)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) bi[36] & 0xff;                        
    ¡¡¡¡¡¡¡¡¡¡System.out.println("Colors
    important are :" + nclrimp);

    ¡¡¡¡¡¡¡¡¡¡if (nbitcount == 24)                                
    ¡¡¡¡¡¡¡¡¡¡{                                                                      
    ¡¡¡¡¡¡¡¡¡¡¡¡// 24 λ¸ñʽ²»°üº¬µ÷É«°åÊý¾Ý£¬µ«É¨ÃèÐб»²¹×ãµ½                  
    ¡¡¡¡¡¡¡¡¡¡¡¡// 4 ¸ö×Ö½Ú¡£                                                    
    ¡¡¡¡¡¡¡¡¡¡¡¡int npad = (nsizeimage /
    nheight) - nwidth * 3;

    ¡¡¡¡¡¡¡¡¡¡¡¡int ndata[]
    = new int[nheight * nwidth];
    ¡¡¡¡¡¡¡¡¡¡¡¡byte brgb[]
    = new byte[ (nwidth + npad)
    * 3 * nheight];
    ¡¡¡¡¡¡¡¡¡¡¡¡fs.read(brgb, 0, (nwidth
    + npad) * 3 * nheight);
    ¡¡¡¡¡¡¡¡¡¡¡¡int nindex = 0;                                        
    ¡¡¡¡¡¡¡¡¡¡¡¡for (int j = 0; j <¡¡
    nheight; j++)
    ¡¡¡¡¡¡¡¡¡¡¡¡{                                                                    
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡for (int i = 0; i <
    ¡¡nwidth; i++)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡{                                                                  
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ndata[nwidth * (nhei
    ght - j - 1) + i] =
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡(255 & 0xff) < <¡¡24            
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (
    ( (int) brgb[nindex + 2] & 0
    xff) < <¡¡16)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( ( (int) brgb
    [nindex + 1] & 0xff) < <¡¡8)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) brgb[nindex] & 0xff;      
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡/**¡¡ System.out.pri
    ntln("Encoded Color at ("
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ +i +
    "," + j + ")is:" + nrgb + "
    (R,G,B)= ("
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ + ( (int) (br
    gb[2]) & 0xff) + ","
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ +
    ( (int) brgb[1] & 0xff) + "
    ,"
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ + ( (int) brg
    b[0] & 0xff) + ")");
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ }*/                                                          
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡nindex += 3;                                          
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡}                                                                  
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡nindex += npad;                                      
    ¡¡¡¡¡¡¡¡¡¡¡¡}                                                                    

    ¡¡¡¡¡¡¡¡¡¡¡¡image = Toolkit.getDefau
    ltToolkit().createImage
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡(new Mem
    oryImageSource(nwidth, nheig
    ht,
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ndata, 0, nwidth));    
    ¡¡¡¡¡¡¡¡¡¡}                                                                      
    ¡¡¡¡¡¡¡¡¡¡else if (nbitcount == 8)                        
    ¡¡¡¡¡¡¡¡¡¡{                                                                      
    ¡¡¡¡¡¡¡¡¡¡¡¡// ±ØÐëÈ·¶¨ÑÕÉ«Êý¡£Èç¹û clrsused ²ÎÊý´óÓÚ 0£¬          
    ¡¡¡¡¡¡¡¡¡¡¡¡// ÔòÑÕÉ«ÊýÓÉËü¾ö¶¨¡£Èç¹ûËüµÈÓÚ 0£¬Ôò¸ù¾Ý                        
    ¡¡¡¡¡¡¡¡¡¡¡¡// bitsperpixel ¼ÆËãÑÕÉ«Êý¡£                          
    ¡¡¡¡¡¡¡¡¡¡¡¡int nNumColors = 0;                                
    ¡¡¡¡¡¡¡¡¡¡¡¡if (nclrused > 0)

    ¡¡¡¡¡¡¡¡¡¡¡¡{                                                                    
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡nNumColors = nclrused;                        
    ¡¡¡¡¡¡¡¡¡¡¡¡}                                                                    
    ¡¡¡¡¡¡¡¡¡¡¡¡else                                                              
    ¡¡¡¡¡¡¡¡¡¡¡¡{                                                                    
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡nNumColors = (1 & 0xff
    ) < <¡¡nbitcount;
    ¡¡¡¡¡¡¡¡¡¡¡¡}                                                                    
    ¡¡¡¡¡¡¡¡¡¡¡¡System.out.println("The
    number of Colors is" + nNumColors);

    ¡¡¡¡¡¡¡¡¡¡¡¡// ijЩλͼ²»¼ÆËã sizeimage Óò£¬ÇëÕÒ³ö                  
    ¡¡¡¡¡¡¡¡¡¡¡¡// ÕâЩÇé¿ö²¢¶ÔËüÃǽøÐÐÐÞÕý¡£                                      
    ¡¡¡¡¡¡¡¡¡¡¡¡if (nsizeimage == 0)                              
    ¡¡¡¡¡¡¡¡¡¡¡¡{                                                                    
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡nsizeimage = ( ( ( (nwidth * nbitcount) + 31) & ~31) >> 3);

    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡nsizeimage *= nheight;                        
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡System.out
    .println("nsizeimage (backup
    ) is" + nsizeimage);
    ¡¡¡¡¡¡¡¡¡¡¡¡}                                                                    

    ¡¡¡¡¡¡¡¡¡¡¡¡// ¶ÁÈ¡µ÷É«°åÑÕÉ«¡£                                                
    ¡¡¡¡¡¡¡¡¡¡¡¡int npalette[] = new int
    [nNumColors];
    ¡¡¡¡¡¡¡¡¡¡¡¡byte bpalett
    e[] = new byte[nNumColors *
    4];
    ¡¡¡¡¡¡¡¡¡¡¡¡fs.read(bpalette, 0, nNu
    mColors * 4);
    ¡¡¡¡¡¡¡¡¡¡¡¡int nindex8 = 0;                                      
    ¡¡¡¡¡¡¡¡¡¡¡¡for (int n = 0; n <¡¡
    nNumColors; n++)
    ¡¡¡¡¡¡¡¡¡¡¡¡{                                                                    
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡npalette[n
    ] = (255 & 0xff) < <¡¡
    24
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( (
    (int) bpalette[nindex8 + 2]
    & 0xff) < <¡¡16)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡| ( (
    (int) bpalette[nindex8 + 1]
    & 0xff) < <¡¡8)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡| (int) bpalette[n
    index8] & 0xff;
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡/**System.out.println
    ("Palette Color "+n
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
    +" is:" + npalette[n] + " (
    res,R,G,B)= ("
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡+ ( (i
    nt) (bpalette[nindex8 + 3])
    & 0xff) + ","
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡+ ( (i
    nt) (bpalette[nindex8 + 2])
    & 0xff) + ","
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡+ ( (i
    nt) bpalette[nindex8 + 1] &
    0xff) + ","
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡+ ( (int) bpalette
    [nindex8] & 0xff) + ")");
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡ */                                                              
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡nindex8 += 4;                                          
    ¡¡¡¡¡¡¡¡¡¡¡¡}                                                                    

    ¡¡¡¡¡¡¡¡¡¡¡¡// ¶ÁȡͼÏñÊý¾Ý£¨Êµ¼ÊÉÏÊǵ÷É«°åµÄË÷Òý£©                            
    ¡¡¡¡¡¡¡¡¡¡¡¡// ɨÃèÐÐÈÔ±»²¹×ãµ½ 4 ¸ö×Ö½Ú¡£                                  
    ¡¡¡¡¡¡¡¡¡¡¡¡int npad8 =
    (nsizeimage / nheight) - nwi
    dth;

    ¡¡¡¡¡¡¡¡¡¡¡¡System.out.p
    rintln("nPad is:" + npad8);

    ¡¡¡¡¡¡¡¡¡¡¡¡int ndata8[] = new int[n
    width * nheight];
    ¡¡¡¡¡¡¡¡¡¡¡¡byte bdata[]
    = new byte[ (nwidth + npad8
    ) * nheight];
    ¡¡¡¡¡¡¡¡¡¡¡¡fs.read(bdat
    a, 0, (nwidth + npad8) * nhe
    ight);
    ¡¡¡¡¡¡¡¡¡¡¡¡nindex8 = 0;                                              

    ¡¡¡¡¡¡¡¡¡¡¡¡for (int j8
    = 0; j8 <¡¡nheight; j8++)
    ¡¡¡¡¡¡¡¡¡¡¡¡{                                                                    
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡for (int i8 = 0; i8 &l
    t;¡¡nwidth; i8++)
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡{                                                                  
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ndata8[nwidth * (nhe
    ight - j8 - 1) + i8] =
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡npalette[ ( (int
    ) bdata[nindex8] & 0xff)];
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡nindex8++;                                              
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡}                                                                  
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡nindex8 += npad8;                                  
    ¡¡¡¡¡¡¡¡¡¡¡¡}                                                                    

    ¡¡¡¡¡¡¡¡¡¡¡¡image = Toolkit.getDefau
    ltToolkit().createImage
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡(new MemoryImageSour
    ce(nwidth, nheight,
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ndata8, 0, nwidth));  
    ¡¡¡¡¡¡¡¡¡¡}                                                                      
    ¡¡¡¡¡¡¡¡¡¡else                                                                
    ¡¡¡¡¡¡¡¡¡¡{                                                                      
    ¡¡¡¡¡¡¡¡¡¡¡¡System.out.println("Not
    aborting...");
    a 24-bit or 8-bit Windows Bitmap,

    ¡¡¡¡¡¡¡¡¡¡¡¡image = (Image)null;                              
    ¡¡¡¡¡¡¡¡¡¡}                                                                      

    ¡¡¡¡¡¡¡¡¡¡fs.close();                                                  
    ¡¡¡¡¡¡¡¡¡¡return image;                                              
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡catch (Exception e)                                    
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡System.out.pri
    ntln("Caught exception in lo
    adbitmap!");
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡return (Image)null;                                    
    ¡¡¡¡¡¡}                                                                          

    ¡¡¡¡¡¡public void waitForImage(Image image)  
    ¡¡¡¡¡¡{                                                                          
    ¡¡¡¡¡¡¡¡//tracker = new MediaTracker(this);    
    ¡¡¡¡¡¡¡¡try                                                                    
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡tracker.addImage(image, 0)
    ; //public void addImage(Image image,int id)
    ¡¡¡¡¡¡¡¡¡¡//tracker.waitForAll();                          
    ¡¡¡¡¡¡¡¡¡¡tracker.waitForID(0);                              
    ¡¡¡¡¡¡¡¡¡¡tracker.checkAll(true);                          
    ¡¡¡¡¡¡¡¡¡¡/** while(!tracker.checkID(0))            
    ¡¡¡¡¡¡¡¡¡¡ {                                                                    
    ¡¡¡¡¡¡¡¡¡¡¡¡ tracker.waitForID(0);                          
    ¡¡¡¡¡¡¡¡¡¡ }*/                                                                
    ¡¡¡¡¡¡¡¡¡¡if (tracker.isErrorAny())                      
    ¡¡¡¡¡¡¡¡¡¡{                                                                      
    ¡¡¡¡¡¡¡¡¡¡¡¡// System.out.println(tr
    acker.getErrorsAny().toString());
    ¡¡¡¡¡¡¡¡¡¡¡¡System.out.println("¼ÓÔØÍ¼Ïñ³öÏÖ´íÎó£¡");      
    ¡¡¡¡¡¡¡¡¡¡¡¡System.exit(0);                                        
    ¡¡¡¡¡¡¡¡¡¡}                                                                      

    ¡¡¡¡¡¡¡¡¡¡// loadStatus = tracker.st
    atusID( 0, false );
    ¡¡¡¡¡¡¡¡¡¡// tracker.removeImage(image, 0);      
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡catch (InterruptedException e)              
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡e.printStackTrace();                                
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡} // waitForImage                                          

    ¡¡¡¡¡¡public void checkImage(Image image)      
    ¡¡¡¡¡¡{                                                                          
    ¡¡¡¡¡¡¡¡waitForImage(image);                                  

    ¡¡¡¡¡¡¡¡int imageWidth = image.getWi
    dth(null);
    ¡¡¡¡¡¡¡¡if (imageWidth <¡¡1)                              
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡throw new IllegalArgumentE
    xception("image width " + imageWidth +
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
    ¡¡¡¡¡¡¡¡ " is out of range");
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡int imageHeight = image.getH
    eight(null);
    ¡¡¡¡¡¡¡¡if (imageHeight <¡¡1)                            
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡throw new Ille
    galArgumentException("image
    height " + imageHeight +
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ " is ou
    t of range");
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡// System.out.println( "Imag
    e size=" + imageWidth + "x" + imageHeight );
    ¡¡¡¡¡¡} // checkImage                                              

    ¡¡¡¡¡¡public void encode
    JPEG(OutputStream outputStre
    am, Image outputImage,
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
    ¡¡¡¡ float outputQuality) th
    rows java.io.IOException
    ¡¡¡¡¡¡{                                                                          
    ¡¡¡¡¡¡¡¡int outputWidth
    = outputImage.getWidth(null)
    ;
    ¡¡¡¡¡¡¡¡if (outputWidth <¡¡1)                            
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡throw new IllegalArgumentE
    xception("output image width " + outputWidth +
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
    ¡¡¡¡¡¡¡¡ " is out of range");
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡int outputHeight
    = outputImage.getHeight(nul
    l);
    ¡¡¡¡¡¡¡¡if (outputHeight <¡¡1)                          
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡throw new IllegalArgumentE
    outputHeight +
    xception("output image height " +

    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡?br>¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ " is ou
    t of range");
    ¡¡¡¡¡¡¡¡}                                                                        


    ¡¡¡¡¡¡¡¡// Get a buffere
    d image from the image.
    ¡¡¡¡¡¡¡¡BufferedImage bi
    = new BufferedImage(outputW
    idth, outputHeight,
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
    ¡¡¡¡¡¡¡¡ BufferedImage.TYPE_INT_RGB);
    ¡¡¡¡¡¡¡¡Graphics2D biContext = bi.cr
    eateGraphics();
    ¡¡¡¡¡¡¡¡biContext.drawImage(outputIm
    age, 0, 0, null);
    ¡¡¡¡¡¡¡¡// Note that additional draw
    here.
    ing such as watermarks or logos can be placed


    ¡¡¡¡¡¡¡¡// com.sun.image.codec.jpeg
    package is included in sun and ibm sdk 1.3
    ¡¡¡¡¡¡¡¡JPEGImageEncoder encoder = J
    PEGCodec.createJPEGEncoder(outputStream);
    ¡¡¡¡¡¡¡¡// The default quality is 0.75.            
    ¡¡¡¡¡¡¡¡JPEGEncodeParam jep = JPEGCo
    dec.getDefaultJPEGEncodeParam(bi);
    ¡¡¡¡¡¡¡¡jep.setQuality(outputQuality, true);  
    ¡¡¡¡¡¡¡¡encoder.encode(bi, jep);                          
    ¡¡¡¡¡¡¡¡// encoder.encode( bi );                          
    ¡¡¡¡¡¡¡¡outputStream.flush();                                
    ¡¡¡¡¡¡} // encodeImage                                            

    ¡¡¡¡¡¡/** Adjusts the si
    ze of the image to the given
    coordinates.
    ¡¡¡¡¡¡ * If width or hei
    ght is -1, the image aspect
    ration is maintained.
    ¡¡¡¡¡¡ * < p>

    ¡¡¡¡¡¡ * Hints are one o
    f SCALE_DEFAULT, SCALE_FAST,
    SCALE_SMOOTH,
    ¡¡¡¡¡¡ * SCALE_REPLICATE
    , SCALE_AREA_AVERAGING as de
    fined in java.awt.Image.
    ¡¡¡¡¡¡ */                                                                      
    ¡¡¡¡¡¡public Image setSize(Image ima
    ge, int width, int height, int hints)
    ¡¡¡¡¡¡{                                                                          
    ¡¡¡¡¡¡¡¡return image.getScaledInstan
    ce(width, height, hints);
    ¡¡¡¡¡¡} // setSize                                                    

    ¡¡¡¡¡¡public Image setSi
    ze(Image image, int width, i
    nt height)
    ¡¡¡¡¡¡{                                                                          
    ¡¡¡¡¡¡¡¡return setSize(image, width,
    //setSizeµ÷ÓõÄÊÇÉÏÒ»¸ö·½·¨.
    height, java.awt.Image.SCALE_DEFAULT);

    ¡¡¡¡¡¡} // setSize                                                    

    ¡¡¡¡¡¡//Component initialization                        
    ¡¡¡¡¡¡public static void main(String[] args)
    ¡¡¡¡¡¡{                                                                          
    ¡¡¡¡¡¡¡¡ScreenPicSaver sps = new Scr
    eenPicSaver();
    ¡¡¡¡¡¡¡¡// String sdir = new String("d:\");  
    ¡¡¡¡¡¡¡¡// String sfile = new String
    ("desktop.bmp");
    ¡¡¡¡¡¡¡¡String outputFil
    eName = new String("d:\desk
    top.jpg");

    ¡¡¡¡¡¡¡¡//Image inputIma
    ge = Toolkit.getDefaultToolk
    it().getImage(inputFileName);
    ¡¡¡¡¡¡¡¡//Image inputIma
    ge = sps.loadbitmap(sdir, sf
    ile);
    ¡¡¡¡¡¡¡¡//==========================
    =====================================
    ¡¡¡¡¡¡¡¡BufferedImage bufferedImage = null;    
    ¡¡¡¡¡¡¡¡Robot robot;                                                  
    ¡¡¡¡¡¡¡¡Image inputImage = null;                          
    ¡¡¡¡¡¡¡¡try                                                                    
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡robot = new Robot();                                
    ¡¡¡¡¡¡¡¡¡¡bufferedImage
    = robot.createScreenCapture(
    new Rectangle(Toolkit.
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡getDefaultToolkit().ge
    ¶ÔÏó
    tScreenSize())); //µÃµ½×ÀÃæÍ¼ÏñµÄbufferedImage


    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡catch (Exception ex)                                  
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡ex.printStackTrace();                              
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡int width = bufferedImage.getWidth();
    ¡¡¡¡¡¡¡¡int height = bufferedImage.g
    etHeight();
    ¡¡¡¡¡¡¡¡inputImage = bufferedImage.g
    etScaledInstance(width, height,
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
    //½«bufferedImage¶ÔÏóת»¯Î»Image¶ÔÏó
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ BufferedImage.TYPE_INT_RGB);


    ¡¡¡¡¡¡¡¡MediaTracker tracker = new M
    ediaTracker(new Component()
    ¡¡¡¡¡¡¡¡{});                                                                  

    ¡¡¡¡¡¡¡¡int outputWidth = inputImage
    Ñù
    .getWidth(null); //ʹµÃÐÂͼÏñºÍԭͼÏñµÄ¿í¶ÈÒ»

    ¡¡¡¡¡¡¡¡float outputQuality = 0.80f;                  
    ¡¡¡¡¡¡¡¡Image outputImag
    ¶ÈÈ·¶¨µÄÇé¿öÏ£¬¸ß¶ÈÔÚÔ­
    e = sps.setSize(inputImage,
    ͼÏñµÄ»ù´¡ÉϳɱÈÀýËõ·Å
    outputWidth, -1); //-1±íʾÔÚ¿í


    ¡¡¡¡¡¡¡¡try                                                                    
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡tracker.addImage(inputImage, 0);        
    ¡¡¡¡¡¡¡¡¡¡//sps.tracker.waitForAll();                  
    ¡¡¡¡¡¡¡¡¡¡tracker.waitForID(0);                              
    ¡¡¡¡¡¡¡¡¡¡tracker.checkAll(true);                          
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡catch (InterruptedException e)              
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡e.printStackTrace();                                
    ¡¡¡¡¡¡¡¡}                                                                        

    ¡¡¡¡¡¡¡¡int imageWidth = inputImage.
    getWidth(null);
    ¡¡¡¡¡¡¡¡if (imageWidth <¡¡1)                              
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡throw new Ille
    galArgumentException("image
    width " + imageWidth +
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
    ¡¡¡¡¡¡¡¡ " is out of range");
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡int imageHeight = inputImage
    .getHeight(null);
    ¡¡¡¡¡¡¡¡if (imageHeight <¡¡1)                            
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡throw new IllegalArgumentE
    xception("image height " + imageHeight +
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
    ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ " is ou
    t of range");
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡try                                                                    
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡FileOutputStre
    am fos = new FileOutputStrea
    m(outputFileName);
    ¡¡¡¡¡¡¡¡¡¡sps.encodeJPEG(fos, output
    Image, outputQuality);
    ¡¡¡¡¡¡¡¡¡¡fos.flush();                                                
    ¡¡¡¡¡¡¡¡¡¡fos.close();                                                
    ¡¡¡¡¡¡¡¡}                                                                        
    ¡¡¡¡¡¡¡¡catch (Exception e)                                    
    ¡¡¡¡¡¡¡¡{                                                                        
    ¡¡¡¡¡¡¡¡¡¡e.printStackTrace();                                
    ¡¡¡¡¡¡¡¡}                                                                        

    ¡¡¡¡¡¡¡¡System.exit(0);                                            

    ¡¡¡¡¡¡}                                                                          

    ¡¡¡¡}                                                                            

    ÉÏÒ»Ò³ ÏÂÒ»Ò³




    map