手机版
你好,游客 登录 注册
背景:
阅读新闻

Java 图片格式转换(包括bmp-->jpg)

[日期:2011-11-05] 来源:Linux社区  作者:shixing_11 [字体:大 中 小]

但对于bmp格式转换成jpg,却没有直接的方法,都是通过第三方包进行转换,下面是参考网上的一篇文章略做了修改后的bmp转jpg的程序:

  1. import Java.awt.Image;  
  2. import java.awt.Toolkit;  
  3. import java.awt.image.BufferedImage;  
  4. import java.awt.image.MemoryImageSource;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import com.sun.image.codec.jpeg.JPEGCodec;  
  9. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  10. public class BmpReader  
  11. {  
  12.     /** 
  13.      * Created on 2010-7-13  
  14.      * <p>Discription:[bmpTojpg]</p> 
  15.      * @param file 
  16.      * @param dstFile 
  17.      */  
  18.     public static void bmpTojpg(String file,String dstFile)  
  19.     {  
  20.         try  
  21.         {  
  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.         }  
  33.         catch (Exception e)  
  34.         {  
  35.             System.out.println(e);  
  36.         }  
  37.     }  
  38.     public static int constructInt(byte[] in, int offset)  
  39.     {  
  40.         int ret = ((int) in[offset + 3] & 0xff);  
  41.         ret = (ret << 8) | ((int) in[offset + 2] & 0xff);  
  42.         ret = (ret << 8) | ((int) in[offset + 1] & 0xff);  
  43.         ret = (ret << 8) | ((int) in[offset + 0] & 0xff);  
  44.         return (ret);  
  45.     }  
  46.     public static int constructInt3(byte[] in, int offset)  
  47.     {  
  48.         int ret = 0xff;  
  49.         ret = (ret << 8) | ((int) in[offset + 2] & 0xff);  
  50.         ret = (ret << 8) | ((int) in[offset + 1] & 0xff);  
  51.         ret = (ret << 8) | ((int) in[offset + 0] & 0xff);  
  52.         return (ret);  
  53.     }  
  54.     public static long constructLong(byte[] in, int offset)  
  55.     {  
  56.         long ret = ((long) in[offset + 7] & 0xff);  
  57.         ret |= (ret << 8) | ((long) in[offset + 6] & 0xff);  
  58.         ret |= (ret << 8) | ((long) in[offset + 5] & 0xff);  
  59.         ret |= (ret << 8) | ((long) in[offset + 4] & 0xff);  
  60.         ret |= (ret << 8) | ((long) in[offset + 3] & 0xff);  
  61.         ret |= (ret << 8) | ((long) in[offset + 2] & 0xff);  
  62.         ret |= (ret << 8) | ((long) in[offset + 1] & 0xff);  
  63.         ret |= (ret << 8) | ((long) in[offset + 0] & 0xff);  
  64.         return (ret);  
  65.     }  
  66.     public static double constructDouble(byte[] in, int offset)  
  67.     {  
  68.         long ret = constructLong(in, offset);  
  69.         return (Double.longBitsToDouble(ret));  
  70.     }  
  71.     public static short constructShort(byte[] in, int offset)  
  72.     {  
  73.         short ret = (short) ((short) in[offset + 1] & 0xff);  
  74.         ret = (short) ((ret << 8) | (short) ((short) in[offset + 0] & 0xff));  
  75.         return (ret);  
  76.     }  
  77.     static class BitmapHeader  
  78.     {  
  79.         public int iSize, ibiSize, iWidth, iHeight, iPlanes, iBitcount, iCompression, iSizeimage, iXpm, iYpm, iClrused, iClrimp;  
  80.         // 读取bmp文件头信息   
  81.         public void read(FileInputStream fs) throws IOException  
  82.         {  
  83.             final int bflen = 14;  
  84.             byte bf[] = new byte[bflen];  
  85.             fs.read(bf, 0, bflen);  
  86.             final int bilen = 40;  
  87.             byte bi[] = new byte[bilen];  
  88.             fs.read(bi, 0, bilen);  
  89.             iSize = constructInt(bf, 2);  
  90.             ibiSize = constructInt(bi, 2);  
  91.             iWidth = constructInt(bi, 4);  
  92.             iHeight = constructInt(bi, 8);  
  93.             iPlanes = constructShort(bi, 12);  
  94.             iBitcount = constructShort(bi, 14);  
  95.             iCompression = constructInt(bi, 16);  
  96.             iSizeimage = constructInt(bi, 20);  
  97.             iXpm = constructInt(bi, 24);  
  98.             iYpm = constructInt(bi, 28);  
  99.             iClrused = constructInt(bi, 32);  
  100.             iClrimp = constructInt(bi, 36);  
  101.         }  
  102.     }  
  103.     public static Image read(FileInputStream fs)  
  104.     {  
  105.         try  
  106.         {  
  107.             BitmapHeader bh = new BitmapHeader();  
  108.             bh.read(fs);  
  109.             if (bh.iBitcount == 24)  
  110.             {  
  111.                 return (readImage24(fs, bh));  
  112.             }  
  113.             if (bh.iBitcount == 32)  
  114.             {  
  115.                 return (readImage32(fs, bh));  
  116.             }  
  117.             fs.close();  
  118.         }  
  119.         catch (IOException e)  
  120.         {  
  121.             System.out.println(e);  
  122.         }  
  123.         return (null);  
  124.     }  
  125.     //24位   
  126.     protected static Image readImage24(FileInputStream fs, BitmapHeader bh) throws IOException  
  127.     {  
  128.         Image image;  
  129.         if (bh.iSizeimage == 0)  
  130.         {  
  131.             bh.iSizeimage = ((((bh.iWidth * bh.iBitcount) + 31) & ~31) >> 3);  
  132.             bh.iSizeimage *= bh.iHeight;  
  133.         }  
  134.         int npad = (bh.iSizeimage / bh.iHeight) - bh.iWidth * 3;  
  135.         int ndata[] = new int[bh.iHeight * bh.iWidth];  
  136.         byte brgb[] = new byte[(bh.iWidth + npad) * 3 * bh.iHeight];  
  137.         fs.read(brgb, 0, (bh.iWidth + npad) * 3 * bh.iHeight);  
  138.         int nindex = 0;  
  139.         for (int j = 0; j < bh.iHeight; j++)  
  140.         {  
  141.             for (int i = 0; i < bh.iWidth; i++)  
  142.             {  
  143.                 ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3(brgb, nindex);  
  144.                 nindex += 3;  
  145.             }  
  146.             nindex += npad;  
  147.         }  
  148.         image = Toolkit.getDefaultToolkit().createImage(  
  149.                 new MemoryImageSource(bh.iWidth, bh.iHeight, ndata, 0, bh.iWidth));  
  150.         fs.close();  
  151.         return (image);  
  152.     }  
  153.     //32位   
  154.     protected static Image readImage32(FileInputStream fs, BitmapHeader bh) throws IOException  
  155.     {  
  156.         Image image;  
  157.         int ndata[] = new int[bh.iHeight * bh.iWidth];  
  158.         byte brgb[] = new byte[bh.iWidth * 4 * bh.iHeight];  
  159.         fs.read(brgb, 0, bh.iWidth * 4 * bh.iHeight);  
  160.         int nindex = 0;  
  161.         for (int j = 0; j < bh.iHeight; j++)  
  162.         {  
  163.             for (int i = 0; i < bh.iWidth; i++)  
  164.             {  
  165.                 ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3(brgb, nindex);  
  166.                 nindex += 4;  
  167.             }  
  168.         }  
  169.         image = Toolkit.getDefaultToolkit().createImage(  
  170.                 new MemoryImageSource(bh.iWidth, bh.iHeight, ndata, 0, bh.iWidth));  
  171.         fs.close();  
  172.         return (image);  
  173.     }  
  174.     public static void main(String[] args)  
  175.     {  
  176.         String srcfile = "c://img//bbb.bmp";  
  177.         String dstFile = "c://img//bbbimg.jpg";  
  178.         bmpTojpg(srcfile,dstFile);  
  179.     }  
  180. }  

只要转成jpg,然后再想转其它格式就OK了~~~

linux
相关资讯       Java教程  Java培训 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款