How to read 8 bits and 24-bit Microsoft Windows bitmaps in the Java application

zhaozj2021-02-08  402

Currently, standard GetImage () methods only support GIF and JPEG images. Although there is a Java routine for reading a PNG (portable network graphic) format, we haven't heard of reading programs for reading Microsoft Windows bitmap images. This trick written by Jeff West provides a code that loads a Windows bitmap image. Java's current release does not officially support reading Microsoft Windows bitmap files in the Java application. But don't worry, we have a way to solve this problem! This tip will explain how to complete this task - we first explain the basic steps to read the Microsoft Windows file format. Windows Dib (Device Separate Bitmap) File format is relatively simple. Unlike the pure bit map format, the DIB format retains the explicit information for storing images in memory. The problem is that variants in the image format are so many (1 bits, 4 bits, 8 bits, and 16 bits, and other formats). The solutions provided in this Java trick use only two formats of 8 bits and 24-bit. These two formats represent the most common variants. Regardless of the Windows Dib subtype, this file format is always consisting of 14 file headers and 40-bit information headers. These two headers exactly contain information about the storage content of the file and the order of storage. For the exact meaning of each of the headers, please refer to Microsoft Software Development Kit (SDK). The content of the file is different from the data in the information head. Let's take a look at the two subtypes you want to process. The 24-bit format is very simple: RGB (red-green-blue) color value (3 bytes, and sorted by BGR) immediately after the information head. However, each scan line is complemented to 4 bytes. According to the description document (see Microsoft SDK), this "complement" is to optimize the Windows bitmap drawing API. At the same time, the scan line of the bottom is the first content in the file - thus relatively conventional graphics coordinate systems (the forward direction of the vector direction is down and right respectively), the image must be read forward. The 8-bit type is complicated due to the insertion palette information between the information head and pixel data. Therefore, each pixel entry is just an 8-bit index of the panel array of 24-bit RGB colors. In pixel information, each scanning is also complemented to 4 bytes. Note that the bitmap image load method provided herein does not support the decompression of the compressed bitmap image. In fact, this routine doesn't even seek this possibility! If you encounter a compressed Windows Dib file, this routine will definitely produce an exception. A description of the compressed Windows DIB format is compressed in Windows SDK, WINDOWS SDK. As for performance, on the 486-DX2-66 MHz system running Microsoft Windows 95, the routine reads 24-bit 640 x 480 The time required for the file (approximately 920 kilobytes) of no more than 10 seconds. Using BufferedInputStream instead of FileInputStream significantly improves performance. The following routines read any of two file formats and generate an image image. The following code does not contain a comprehensive error and exception handling to avoid making this routine more complicated. You can always convert unsupported Windows DIB subtypes with Windows Paint programs. / ** loadBitmap () method is converted by Windows C code. Only uncompressed 24-bit and 8-bit images can be read. The image saved with Microsoft Paint with Microsoft Paint is tested on Windows 95. If the image is not a 24-bit or 8-bit image, the program refuses to make any attempts.

I guess if the mask operation is performed with 1100, and then use 0011 to perform the mask operation, the 4-digit image can also be included. I actually not interested in these images. If you try to read a compressed image, the routine may fail and generate an oewception exception. If the variable NCompRESSION is not 0, it is indicated that it has been compressed. Parameters: SDIR and SFile are the results of FileDialog's getDirectory () and getFile () methods. Return Value: Image object, remember to check (image) null !!!! * / public image loadbitmap (string sdir, string sfile) {image image; system.out.println ("load:" sdir sfile; try {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 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)) [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)) = ((int)) = (((int)) = INT) Bi [18]) << 16) | ((int) BI [17]) << 8) | (int) BI [1 6]; 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)) 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 0) {nnumcolors = nclrused;} else {nnumcolors = (1 & 0xFF) << nbit;} system.out.println ("The number of colors is" nnumcolors; // Some The bitmap does 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 palette color.

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

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

New Post(0)