Implement the print screen with Java, read bitmap files, transform bitmap files in JPEG format and save to file output!

xiaoxiao2021-03-06  78

Package screensaver;

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.image.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 component () {};

Public screenpicsaver () {}

Public Image Loadbitmap (String Sdir, String Sfile) // Reads 8-bit or 24-bit bitmap file {image image; system.out.println ("Loading:" SDIR SFILE); tryinputstream fs = new fileInputstream FS = New FileInputStream (SDIR SFILE); int bflen = 14; // 14 byte BitmapfileHeader Byte bf [] = new byte [bflen]; fs.read (bf, 0, bflen); int bilen = 40; // 40 byte BitmapInfoHeader BYTE BI [] = New byte [Bilen]; fs.read (bi, 0, bilen);

// Interpret the data. INT nsize = ((int) bf [5] & 0xff) << 24) | ((int) bf [4] & 0xff) << 16) | ((int) BF [3] & 0xFF) < <8) | (int) BF [2] & 0xff; System.out.Println ("File Type IS:" (Char) BF [0] (Char) BF [1]); System.out.Println "SIZE OF FILE IS:" nsize);

INT nbisize = ((int) BI [3] & 0xFF) << 24) | ((int) Bi [2] & 0xFF) << 16) | (((int) Bi [1] & 0xFF) < <8) | (int) BI [0] & 0xff; System.out.println ("Size of BitmapInfoHeader IS:" Nbisize);

INT NWIDTH = ((int) BI [7] & 0xFF) << 24) | ((int) BI [6] & 0xFF) << 16) | ((int) Bi [5] & 0xFF) < <8) | (int) BI [4] & 0xff; system.out.println ("width is:" nwidth; int NHEIGHT = ((int) BI [11] & 0xff) << 24) | ( (INT) BI [10] & 0xFF) << 16) | ((int) Bi [9] & 0xFF) << 8) | (int) Bi [8] & 0xff; system.out.println (" Height IS: " NHEIGHT);

INT NPLANES = ((int) BI [13] & 0xFF) << 8) | (int) bi [12] & 0xff; System.out.Println ("Plaso");

INT nbitcount = ((int) BI [15] & 0xFF) << 8) | (int) bi [14] & 0xff; system.out.println ("Bitcount IS:" nbitcount;

/ / Find a non-zero value int ncompression = ((int) bi [19]) << 24) | ((int) BI [18]) << 16) | ((int)) ]) << 8) | (int) bi [16]; system.out.println ("Compression IS: NCompRESSION);

INT nsizeImage = ((int) BI [23] & 0xff) << 24) | ((int) BI [22] & 0xFF) << 16) | ((int) Bi [21] & 0xFF) < <8) | (int) BI [20] & 0xff; System.out.println ("SizeImage IS: NSizeImage);

INT NXPM = ((int) BI [27] & 0xFF) << 24) | ((int) Bi [26] & 0xff) << 16) | ((int) BI [25] & 0xFF) < <8) | (int) Bi [24] & 0xff; System.out.Println ("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-Pixels Per meter is:" NYPM); int nclrused = ((int) Bi [35] & 0xff) << 24) | ((int) BI [34] & 0xff) << 16) | ((int) BI [33] & 0xFF) << 8) | (int) BI [32] & 0xFF; System.out .println ("Colors Used Are: NCLRUSED);

INT nclrimp = ((int) BI [39] & 0xFF) << 24) | ((int) BI [38] & 0xFF) << 16) | ((int) BI [37] & 0xFF) < <8) | (int) BI [36] & 0xFF; System.out.println ("Colors Important Are: NClrim);

If (nbitcount == 24) {// 24-bit format does not include palette data, but the scanning is complement to // 4 bytes. INT NPAD = (nsizeImage / nHEight) - nwidth * 3; int nData [] = new int =NT [NHEIGHT * NWIDTH]; BYTE brGb [] = new byte [(nwidth nPAD) * 3 * nHEight]; fs.read (brgb , 0, (NWIDTH NPAD); INT NINDEX = 0; for (int J = 0; j

Image = Toolkit.getDefaultToolkit (). CreateImage (NWidth, NHEIGHT, NDATA, 0, NWIDTH));} else if (nbitcount == 8) {// must determine the number of colors. If the clrsused parameter is greater than 0, the number of colors is determined by it. If it is equal to 0, calculate the number of colors according to // bitsperpixel. INT nnumcolors = 0; if (nclrused> 0) {nnumcolors = nclrused;} else {nnumcolors = (1 & 0xFF) << nbit;} system.out.println ("The Number of Colors Is" nnumcolors; // Some bitmaps do not calculate the SizeImage domain, find out // These situations and correct them. IF (nsizeImage = ((((nwidth * nbitcount) 31) & ~ 31) >> 3); nsizeImage * = NHEIGHT; system.out.println ("NSIZEIMAGE (Backup) NSIZEIMAGE }

// Read the color of the palette. Int npalette [] = new int [nnumcolors]; byte bPALETTE [] = new byte [nnumcolors * 4]; fs.read (bPALETTE, 0, NNUMCOLORS * 4); int NINDEX8 = 0; for (int N = 0; N

INT NDATA8 [] = new int [nwidth * nheight]; byte bdata [] = new byte [(nwidth nPad8) * nHEight]; fs.read (bdata, 0, (nwidth nPad8) * nHEight); NINDEX8 = 0 For (int J8 = 0; J8

fs.close (); return image;} catch (exception e) {system.out.println ("Caught Exception in LoadBitmap!");} 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 (Tracker.GeterRorsany (). TOSTRING ()); System.out.println ("Load Image An error!"); system.exit (0);}

// loadStatus = tracker.statusid (0, false); // tracker.removeImage (image, 0);} catch (interruptedExcection e) {E.PrintStackTrace ();}} // WaitforImage

public void checkImage (Image image) {waitForImage (image); int imageWidth = image.getWidth (null); if (imageWidth <1) {throw new IllegalArgumentException ( "image width" imageWidth "is out of range");} int imageHeight = image.getHeight (null); if (imageHeight <1) {throw new IllegalArgumentException ( "image height" imageHeight "is out of range");} // System.out.println ( "Image size =" ImageWidth "X" ImageHeight);} // CheckImage

public void encodeJPEG (OutputStream outputStream, Image outputImage, float outputQuality) throws java.io.IOException {int outputWidth = outputImage.getWidth (null); if (outputWidth <1) {throw new IllegalArgumentException ( "output image width" outputWidth " is out of range ");} int outputHeight = outputImage.getHeight (null); if (outputHeight <1) {throw new IllegalArgumentException (" output image height " outputHeight " is out of range ");}

// Get a buffered image from the image BufferedImage bi = new BufferedImage (outputWidth, outputHeight, BufferedImage.TYPE_INT_RGB);. Graphics2D biContext = bi.createGraphics (); biContext.drawImage (outputImage, 0, 0, null); // Note That Additional Drawing Such As Watermarks or logos can be beast here.

// com.sun.image.codec.jpeg package is included in sun and ibm sdk 1.3 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (outputStream); // The default quality is 0.75 JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam (bi);. Jep. Setquality (outputquality, true); Encoder.Encode (BI, JEP); // encoder.encode (bi); OutputStream.flush ();} // encodeImage / ** Adjusts the size of the image to the given coordinates * If width or height is -1, the image aspect ration is maintained. *

* Hints are one of SCALE_DEFAULT, SCALE_FAST, SCALE_SMOOTH, * SCALE_REPLICATE, SCALE_AREA_AVERAGING as defined in java.awt.Image. * / public Image setSize (Image Image, int width, int hent, int hints {return image.getscaledinstance (width, height, hints);} // setsize

Public Image Setsize (Image Image, Int Width, INTHETIGHT) {Return Setsize (image, width, height, java.awt.Image.scale_default); // setsize call is the last method.} // setsize

// Component Initialization Public Static Void Main (String [] args) {screenpicsaver sps = new screenpicsaver (); // string sdir = new string ("d: //"); // String sfile = new string ("Desktop. BMP "); String OutputFileName = New String (" D: //desktop.jpg ");

// Image INPUTIMAGE = Toolkit.getDefaultToolkit (). GetImage (InputFileName); // Image InputImage = SPS.LOADBITMAP (SDIR, SFILE); // ================== ====================================================== BufferedImage BufferedImage = NULL; Robot robot; image inputImage = null; try {robot = new Robot (); bufferedImage = robot.createScreenCapture (new Rectangle (Toolkit getDefaultToolkit () getScreenSize ())..); // get a desktop image objects BufferedImage} catch (Exception ex) {ex.printStackTrace ();} int width = bufferedImage.getWidth (); int height = bufferedImage.getHeight (); inputImage = bufferedImage.getScaledInstance (width, height, BufferedImage.TYPE_INT_RGB); // bit to convert the object bufferedImage Image object

Mediatracker Tracker = New MediaTracker (New Component () {};

INT OUTPUTWIDTH = INPUTIMAGE.GETWIDTH (NULL); // Make the new image and the width of the original image float outputquality = 0.80f; Image OutputImage = SPS.SETSIZE (INPUTIMAGE, OUTPUTWIDTH, -1); // - 1 Indicates in the width determination In the case of a proportional scale on the basis of the original image

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 IllegalArgumentException ( "image width" imageWidth "is out of range");} int imageHeight = inputImage.getHeight (null); if (imageHeight <1) {throw new IllegalArgumentException ( "image height" imageHeight "is out of range");} try {FileOutputStream fos = new FileOutputStream (outputFileName); sps.encodeJPEG (fos, outputImage, outputQuality); fos.flush ( ); Fos.close ();} catch (exception e) {E.PrintStackTrace ();} system.exit (0);

}

}

转载请注明原文地址:https://www.9cbs.com/read-92877.html

New Post(0)