Here is one of the ways to convert the BMP format to JPG format:
1. Package net.oschina.tester;
2.
3. Import Java.awt.Image;
4. Import Java.awt.Toolkit;
5. Import Java.awt.image.BufferedImage;
6. Import Java.awt.image.MemoryImageSource;
7. Import Java.io.FileInputStream;
8. Import Java.io.FileOutputStream;
9. Import Java.io.IOException;
. import Com.sun.image.codec.jpeg.JPEGCodec;
One. Import Com.sun.image.codec.jpeg.JPEGImageEncoder;
12.
public class Bmpreader {
14.
15./**
16. * Image format Conversion BMP-JPG
* @param file
* @param dstfile
19. */
public static void Bmptojpg (string file, String dstfile) {
. try {
FileInputStream in = new FileInputStream (file);
Theimage Image = read (in);
. int wideth = Theimage.getwidth (null);
. int height = theimage.getheight (null);
BufferedImage tag = new BufferedImage (Wideth, HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB);
Tag.getgraphics (). DrawImage (theimage, 0, 0, wideth, height, null);
FileOutputStream out = new FileOutputStream (dstfile);
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (tag);
Out.close ();
.} catch (Exception e) {
System.out.println (e);
34.}
35.}
36.
Notoginseng. public static int Constructint (byte[] in, int offset) {
. int ret = ((int) In[offset + 3] & 0xff);
(ret << 8) | (int) In[offset + 2] & 0xff);
+ ret = (ret << 8) | (int) In[offset + 1] & 0xff);
A. ret = (ret << 8) | (int) In[offset + 0] & 0xff);
A. return (ret);
43.}
44.
public static int ConstructInt3 (byte[] in, int offset) {
. int ret = 0xFF;
ret = (ret << 8) | (int) In[offset + 2] & 0xff);
ret = (ret << 8) | (int) In[offset + 1] & 0xff);
. RET = (ret << 8) | (int) In[offset + 0] & 0xff);
. return (ret);
51.}
52.
Constructlong public static long = (byte[] in, int offset) {
A. Long ret = ((long) In[offset + 7] & 0xff);
|= ret (ret << 8) | (long) In[offset + 6] & 0xff);
|= ret (ret << 8) | (long) In[offset + 5] & 0xff);
|= ret (ret << 8) | (long) In[offset + 4] & 0xff);
|= ret (ret << 8) | (long) In[offset + 3] & 0xff);
|= ret (ret << 8) | (long) In[offset + 2] & 0xff);
RET |= (ret << 8) | (long) In[offset + 1] & 0xff);
|= ret (ret << 8) | (long) In[offset + 0] & 0xff);
. return (ret);
63.}
64.
. public static double constructdouble (byte[] in, int offset) {
A. Long ret = Constructlong (in, offset);
(double.longbitstodouble) return (ret);
68.}
69.
. public static short Constructshort (byte[] in, int offset) {
Short ret = (short) ((short) In[offset + 1] & 0xff);
A. ret = (short) (ret << 8) | (short) (short) In[offset + 0] & 0xff));
return (ret);
74.}
75.
The static class Bitmapheader {
A. public int isize, ibisize, iwidth, Iheight, Iplanes, Ibitcount,
Icompression, Isizeimage, ixpm, IYPM, iclrused, Iclrimp;
79.
80.//Read BMP file header information
Bayi. public void Read (FileInputStream fs) throws IOException {
Bflen final int = 14;
. Byte bf[] = new Byte[bflen];
Fs.read (BF, 0, Bflen);
Bilen final int = 40;
*. Byte bi[] = new Byte[bilen];
Fs.read (BI, 0, Bilen);
Isize = Constructint (BF, 2);
Ibisize = Constructint (bi, 2);
iwidth = Constructint (BI, 4);
iheight = Constructint (bi, 8);
Iplanes = Constructshort (bi, 12);
Ibitcount = Constructshort (bi, 14);
94. Icompression = Constructint (bi, 16);
Isizeimage = Constructint (bi, 20);
ixpm = Constructint (bi, 24);
IYPM = Constructint (bi, 28);
98. iclrused = Constructint (bi, 32);
Iclrimp = Constructint (bi, 36);
100.}
101.}
102.
103. public static Image Read (FileInputStream FS) {
104. Try {
Bitmapheader bh = new Bitmapheader ();
106. Bh.read (FS);
107. if (Bh.ibitcount = = 24) {
108. Return (READIMAGE24 (FS, BH));
109.}
if (Bh.ibitcount = = 32) {
111. Return (READIMAGE32 (FS, BH));
112.}
113. Fs.close ();
A.} catch (IOException e) {
System.out.println (e);
116.}
117. return (NULL);
118.}
119.
120.//24-bit
121. Protected static Image ReadImage24 (FileInputStream FS, Bitmapheader BH)
122. Throws IOException {
123. Image Image;
124. if (Bh.isizeimage = = 0) {
Bh.isizeimage = ((((Bh.iwidth * bh.ibitcount) + & ~31) >> 3);
126. Bh.isizeimage *= Bh.iheight;
127.}
Npad int = (bh.isizeimage/bh.iheight)-bh.iwidth * 3;
129. int ndata[] = new int[bh.iheight * Bh.iwidth];
brgb[byte] = new byte[(bh.iwidth + npad) * 3 * bh.iheight];
131. Fs.read (Brgb, 0, (bh.iwidth + npad) * 3 * bh.iheight);
nindex int = 0;
133. for (int j = 0; J < Bh.iheight; J + +) {
134. for (int i = 0; i < bh.iwidth; i++) {
135. Ndata[bh.iwidth * (BH.IHEIGHT-J-1) + i] = ConstructInt3 (
136. Brgb, nindex);
137. Nindex + = 3;
138.}
139. nindex + = Npad;
140.}
141. Image = Toolkit.getdefaulttoolkit (). CreateImage (
142. New MemoryImageSource (Bh.iwidth, Bh.iheight, Ndata, 0,
143. Bh.iwidth));
144. Fs.close ();
145. return (image);
146.}
147.
148.//32-bit
149. Protected static Image ReadImage32 (FileInputStream FS, Bitmapheader BH)
Throws IOException {
151. Image Image;
int ndata[] = new int[bh.iheight * Bh.iwidth];
153. Byte brgb[] = new Byte[bh.iwidth * 4 * bh.iheight];
154. Fs.read (Brgb, 0, Bh.iwidth * 4 * bh.iheight);
155. int nindex = 0;
156. for (int j = 0; J < Bh.iheight; J + +) {
157. for (int i = 0; i < bh.iwidth; i++) {
158. Ndata[bh.iwidth * (BH.IHEIGHT-J-1) + i] = ConstructInt3 (
159. Brgb, nindex);
Nindex + = 4;
161.}
162.}
163. Image = Toolkit.getdefaulttoolkit (). CreateImage (
164. New MemoryImageSource (Bh.iwidth, Bh.iheight, Ndata, 0,
165. Bh.iwidth));
166. Fs.close ();
167. return (image);
168.}
169.
The public static void main (string[] args) {
171. String srcfile = "D:\\1.bmp";
172. String Dstfile = "D:\\1.jpg";
173. Bmptojpg (Srcfile, dstfile);
174.}
175.
176.}
These are the use of Java programming to convert BMP format to JPG format of a method, of course, we have other methods can be added to add, we come together to learn progress, if you do not understand the friend can add me Q, or add group number to learn together, we learn programming sharing video, Hope to help friends who like Java. If you need any help, you can contact me.
Convert BMP format to JPG format in Java