熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

printscreen讀取位圖轉為jpeg格式輸出

2022-06-13   來源: JSP教程 

  package screensaver;
  
  import javaawt*;
  import javaawtimageBufferedImage;
  import javaio*;
  import javautil*;
  import decjpeg*;
  import javaawtImage;
  import javaawtToolkit;
  import javaawtimageBufferedImage;
  import javaawtimageMemoryImageSource;
  import javaioFileInputStream;
  import javaioFileOutputStream;
  import decjpegJPEGCodec;
  import decjpegJPEGImageEncoder;
  
  public class ScreenPicSaver
  {
   MediaTracker tracker = new MediaTracker(new Component()
   {});
  
   public ScreenPicSaver()
   {}
  
   public Image loadbitmap(String sdir String sfile) //讀取位或位的位圖文件
   {
    Image image;
    Systemoutprintln(loading: + sdir + sfile);
    try
    {
     FileInputStream fs = new FileInputStream(sdir + sfile);
     int bflen = ; // 字節 BITMAPFILEHEADER
     byte bf[] = new byte[bflen];
     fsread(bf bflen);
     int bilen = ; // 字節 BITMAPINFOHEADER
     byte bi[] = new byte[bilen];
     fsread(bi bilen);
  
     // 解釋數據
     int nsize = ( ( (int) bf[] & xff) < < )
       | ( ( (int) bf[] & xff) < < )
       | ( ( (int) bf[] & xff) < < )
       | (int) bf[] & xff;
     Systemoutprintln(File type is : + (char) bf[] + (char) bf[]);
     Systemoutprintln(Size of file is : + nsize);
  
     int nbisize = ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | (int) bi[] & xff;
     Systemoutprintln(Size of bitmapinfoheader is : + nbisize);
  
     int nwidth = ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | (int) bi[] & xff;
     Systemoutprintln(Width is : + nwidth);
  
     int nheight = ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | (int) bi[] & xff;
     Systemoutprintln(Height is : + nheight);
  
     int nplanes = ( ( (int) bi[] & xff) < < ) | (int) bi[] & xff;
     Systemoutprintln(Planes is : + nplanes);
  
     int nbitcount = ( ( (int) bi[] & xff) < < ) | (int) bi[] & xff;
     Systemoutprintln(BitCount is : + nbitcount);
  
     // 查找表明壓縮的非零值
     int ncompression = ( ( (int) bi[]) < < )
       | ( ( (int) bi[]) < < )
       | ( ( (int) bi[]) < < )
       | (int) bi[];
     Systemoutprintln(Compression is : + ncompression);
  
     int nsizeimage = ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | (int) bi[] & xff;
     Systemoutprintln(SizeImage is : + nsizeimage);
  
     int nxpm = ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | (int) bi[] & xff;
     Systemoutprintln(XPixels per meter is : + nxpm);
  
     int nypm = ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | (int) bi[] & xff;
     Systemoutprintln(YPixels per meter is : + nypm);
  
     int nclrused = ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | (int) bi[] & xff;
     Systemoutprintln(Colors used are : + nclrused);
  
     int nclrimp = ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | ( ( (int) bi[] & xff) < < )
       | (int) bi[] & xff;
     Systemoutprintln(Colors important are : + nclrimp);
  
     if (nbitcount == )
     {
      // 位格式不包含調色板數據但掃描行被補足到
      // 個字節
      int npad = (nsizeimage / nheight) nwidth * ;
      int ndata[] = new int[nheight * nwidth];
      byte brgb[] = new byte[ (nwidth + npad) * * nheight];
      fsread(brgb (nwidth + npad) * * nheight);
      int nindex = ;
      for (int j = ; j < nheight; j++)
      {
       for (int i = ; i < nwidth; i++)
       {
        ndata[nwidth * (nheight j ) + i] =
          ( & xff) < < 
          | ( ( (int) brgb[nindex + ] & xff) < < )
          | ( ( (int) brgb[nindex + ] & xff) < < )
          | (int) brgb[nindex] & xff;
        /**  Systemoutprintln(Encoded Color at (
          +i + + j + )is: + nrgb + (RGB)= (
            + ( (int) (brgb[]) & xff) +
            + ( (int) brgb[] & xff) +
            + ( (int) brgb[] & xff) + ));
         }*/
        nindex += ;
       }
       nindex += npad;
      }
  
      image = ToolkitgetDefaultToolkit()createImage
        (new MemoryImageSource(nwidth nheight
                    ndata nwidth));
     }
     else if (nbitcount == )
     {
      // 必須確定顏色數如果 clrsused 參數大於
      // 則顏色數由它決定如果它等於 則根據
      // bitsperpixel 計算顏色數
      int nNumColors = ;
      if (nclrused > )
      {
       nNumColors = nclrused;
      }
      else
      {
       nNumColors = ( & xff) < < nbitcount;
      }
      Systemoutprintln(The number of Colors is + nNumColors);
  
      // 某些位圖不計算 sizeimage 域請找出
      // 這些情況並對它們進行修正
      if (nsizeimage == )
      {
       nsizeimage = ( ( ( (nwidth * nbitcount) + ) & ~) >> );
       nsizeimage *= nheight;
       Systemoutprintln(nsizeimage (backup) is + nsizeimage);
      }
  
      // 讀取調色板顏色
      int npalette[] = new int[nNumColors];
      byte bpalette[] = new byte[nNumColors * ];
      fsread(bpalette nNumColors * );
      int nindex = ;
      for (int n = ; n < nNumColors; n++)
      {
       npalette[n] = ( & xff) < < 
         | ( ( (int) bpalette[nindex + ] & xff) < < )
         | ( ( (int) bpalette[nindex + ] & xff) < < )
         | (int) bpalette[nindex] & xff;
       /**Systemoutprintln (Palette Color +n
             + is: + npalette[n] + (resRGB)= (
         + ( (int) (bpalette[nindex + ]) & xff) +
         + ( (int) (bpalette[nindex + ]) & xff) +
         + ( (int) bpal
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19747.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.