How to read 8 digits in the Java application, 24 Microsoft Windows bitmap (boutique)

zhaozj2021-02-16  61

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 ( "loading:" sdir sfile); try {FileInputStream fs = new FileInputStream (sdir sfile); int bflen = 14; // 14 BiTMAPFILEHEADERBYTE BF [] = new byte [bflen]; fs.read (bf, 0, bflen); int bilen = 40; // 40 byte BitmapInfoHeaderbyTe 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 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

Image = CreateImage (NWidth, NHEIGHT, NDATA8, 0, NWIDTH));} else {system.out.println ("NOT A 24-Bit or 8-bit Windows Bitmap, Aborting ..."); Image = (IMage) null;}

fs.close (); return image;} catch (exception e) {system.out.println ("Caught Exception in LoadBitmap!");} return (image) null;}

You have mastered the skills of reading a bitmap file. It is easy to expand this method, allowing it to read monochrome and 16 color (4-bit) format.

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

New Post(0)