Read 24-bit BMP images and generate JPG thumbnails (1)

zhaozj2021-02-17  140

In any comprehensive website, we often need to upload some picture information. However, with the popularity of high resolution DC, the uploaded picture capacity will be large, such as 3 million pixel DCs out of 600K. For convenience of management, everyone may not be willing to modify it with ACDSEE each time, and upload directly to the server. But this approach is not so easy in the client, and users who dial-up Internet are simply a dream, although you can set wide and high in the image area! Previous article (http://www.9cbs.net/develop/read_article.asp?id=27417) I mainly introduced JPG (JEPG), GIF, PNG image reading and compression method, unfortunately Support BMP images. This article is a drum, and make persistent efforts to solve this problem in one fell swoop! code

Package BMP;

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 bmpreader {

Public string reader () {image image; // Constructs a target map string result = ""; // Return the result try {fileInputstream fs = new fileInputstream ("/ test.bmp"); int bflen = 14; byte bf [] = new byte [bflen]; fs.read (bf, 0, bflen); // read 14-byte BMP file head int bilen = 40; Byte Bi [] = new byte [bilen]; fs.read (bi, 0, bilen; // read 40 bytes BMP information head

/ / Get some important data int nwidth = ((int) Bi [7] & 0xff) << 24) // Source Map Width | ((int) BI [6] & 0xFF) << 16) | (((INT) ) Bi [5] & 0xFF) << 8) | (int) Bi [4] & 0xff; system.out.println ("Wid:" nwidth); int nHeiGHT = ((int) Bi [11] & 0xff) < <24) // Source Map Height | ((int) Bi [10] & 0xFF) << 16) | ((int) BI [9] & 0xFF) << 8) | (int) BI [8] & 0xFF; System.out.println ("High:" NHEight); // Bit INT Nbitcount = ((int) BI [15] & 0xFF) << 8) | (int) BI [14] & 0xff; system.out. Println ("number:" nbitcount); // Source map size int nsizeImage = ((int) Bi [23] & 0xff) << 24) | ((int) Bi [22] & 0xff) << 16) ((int) BI [21] & 0xFF) << 8) | (int) BI [20] & 0xff; System.out.Println ("Source Map:" nsizeImage;

// Analyze IF for 24-bit BMP (nbitcount == 24) {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

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

New Post(0)