將BMP格式用Java轉換成JPG格式
下面是將BMP格式轉換成JPG格式的其中一種方法:
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;
10. import com.sun.image.codec.jpeg.JPEGCodec;
11. import com.sun.image.codec.jpeg.JPEGImageEncoder;
12.
13. public class BmpReader {
14.
15. /**
16. * 圖片格式轉換 BMP -> JPG
17. * @param file
18. * @param dstFile
19. */
20. public static void bmpTojpg(String file, String dstFile) {
21. try {
22. FileInputStream in = new FileInputStream(file);
23. Image TheImage = read(in);
24. int wideth = TheImage.getWidth(null);
25. int height = TheImage.getHeight(null);
26. BufferedImage tag = new BufferedImage(wideth, height,BufferedImage.TYPE_INT_RGB);
27. tag.getGraphics().drawImage(TheImage, 0, 0, wideth, height, null);
28. FileOutputStream out = new FileOutputStream(dstFile);
29. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
30. encoder.encode(tag);
31. out.close();
32. } catch (Exception e) {
33. System.out.println(e);
34. }
35. }
36.
37. public static int constructInt(byte[] in, int offset) {
38. int ret = ((int) in[offset + 3] & 0xff);
39. ret = (ret << 8) | ((int) in[offset + 2] & 0xff);
40. ret = (ret << 8) | ((int) in[offset + 1] & 0xff);
41. ret = (ret << 8) | ((int) in[offset + 0] & 0xff);
42. return (ret);
43. }
44.
45. public static int constructInt3(byte[] in, int offset) {
46. int ret = 0xff;
47. ret = (ret << 8) | ((int) in[offset + 2] & 0xff);
48. ret = (ret << 8) | ((int) in[offset + 1] & 0xff);
49. ret = (ret << 8) | ((int) in[offset + 0] & 0xff);
50. return (ret);
51. }
52.
53. public static long constructLong(byte[] in, int offset) {
54. long ret = ((long) in[offset + 7] & 0xff);
55. ret |= (ret << 8) | ((long) in[offset + 6] & 0xff);
56. ret |= (ret << 8) | ((long) in[offset + 5] & 0xff);
57. ret |= (ret << 8) | ((long) in[offset + 4] & 0xff);
58. ret |= (ret << 8) | ((long) in[offset + 3] & 0xff);
59. ret |= (ret << 8) | ((long) in[offset + 2] & 0xff);
60. ret |= (ret << 8) | ((long) in[offset + 1] & 0xff);
61. ret |= (ret << 8) | ((long) in[offset + 0] & 0xff);
62. return (ret);
63. }
64.
65. public static double constructDouble(byte[] in, int offset) {
66. long ret = constructLong(in, offset);
67. return (Double.longBitsToDouble(ret));
68. }
69.
70. public static short constructShort(byte[] in, int offset) {
71. short ret = (short) ((short) in[offset + 1] & 0xff);
72. ret = (short) ((ret << 8) | (short) ((short) in[offset + 0] & 0xff));
73. return (ret);
74. }
75.
76. static class BitmapHeader {
77. public int iSize, ibiSize, iWidth, iHeight, iPlanes, iBitcount,
78. iCompression, iSizeimage, iXpm, iYpm, iClrused, iClrimp;
79.
80. // 讀取bmp文件頭信息
81. public void read(FileInputStream fs) throws IOException {
82. final int bflen = 14;
83. byte bf[] = new byte[bflen];
84. fs.read(bf, 0, bflen);
85. final int bilen = 40;
86. byte bi[] = new byte[bilen];
87. fs.read(bi, 0, bilen);
88. iSize = constructInt(bf, 2);
89. ibiSize = constructInt(bi, 2);
90. iWidth = constructInt(bi, 4);
91. iHeight = constructInt(bi, 8);
92. iPlanes = constructShort(bi, 12);
93. iBitcount = constructShort(bi, 14);
94. iCompression = constructInt(bi, 16);
95. iSizeimage = constructInt(bi, 20);
96. iXpm = constructInt(bi, 24);
97. iYpm = constructInt(bi, 28);
98. iClrused = constructInt(bi, 32);
99. iClrimp = constructInt(bi, 36);
100. }
101. }
102.
103. public static Image read(FileInputStream fs) {
104. try {
105. BitmapHeader bh = new BitmapHeader();
106. bh.read(fs);
107. if (bh.iBitcount == 24) {
108. return (readImage24(fs, bh));
109. }
110. if (bh.iBitcount == 32) {
111. return (readImage32(fs, bh));
112. }
113. fs.close();
114. } catch (IOException e) {
115. System.out.println(e);
116. }
117. return (null);
118. }
119.
120. // 24位
121. protected static Image readImage24(FileInputStream fs, BitmapHeader bh)
122. throws IOException {
123. Image image;
124. if (bh.iSizeimage == 0) {
125. bh.iSizeimage = ((((bh.iWidth * bh.iBitcount) + 31) & ~31) >> 3);
126. bh.iSizeimage *= bh.iHeight;
127. }
128. int npad = (bh.iSizeimage / bh.iHeight) - bh.iWidth * 3;
129. int ndata[] = new int[bh.iHeight * bh.iWidth];
130. byte brgb[] = new byte[(bh.iWidth + npad) * 3 * bh.iHeight];
131. fs.read(brgb, 0, (bh.iWidth + npad) * 3 * bh.iHeight);
132. int nindex = 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位
149. protected static Image readImage32(FileInputStream fs, BitmapHeader bh)
150. throws IOException {
151. Image image;
152. 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);
160. 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.
170. 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. }
這些是用JAVA編程將BMP格式轉換成JPG格式的一種方法,當然大家還有其它的方法可以添加補充進來,大家一起來學習進步,如有不懂的朋友可以加我Q,或者加群號來一起學習,大家一起學習編程分享視頻,希望能幫助喜歡JAVA的朋友。有需要幫助的也可以聯系我。
將BMP格式用Java轉換成JPG格式