楼主: chenaidszzz
2111 0

将BMP格式用Java转换成JPG格式 [推广有奖]

  • 0关注
  • 0粉丝

本科生

66%

还不是VIP/贵宾

-

威望
0
论坛币
20 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
575 点
帖子
55
精华
0
在线时间
17 小时
注册时间
2017-9-20
最后登录
2017-10-30

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  下面是将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的朋友。有需要帮助的也可以联系我。

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Java BMP jav JPG exception

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-27 07:46