Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 681|回复: 0

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

[复制链接]

该用户从未签到

发表于 2011-9-18 13:51:54 | 显示全部楼层 |阅读模式
java在做图片类型转换时,JDK本身提供的类对 ??GIF->JPG   GIF-&gtNG   PNG->GIF(X)   PNG->JPG
这几种类型的转换支持比较好, 可以通过类直接转,代码如下:

     /**
      * Created on 2010-7-13  
      * Discription:[convert GIF->JPG GIF->PNG PNG->GIF(X) PNG->JPG ]
      * @param source
      * @param formatName
      * @param result
      * @author:[shixing_11@sina.com]
      */  
     public static void convert(String source, String formatName, String result)  
     {  
         try  
         {  
             File f = new File(source);  
             f.canRead();  
             BufferedImage src = ImageIO.read(f);  
             ImageIO.write(src, formatName, new File(result));  
         }  
         catch (Exception e)  
         {  
             e.printStackTrace();  
         }  
      }
但对于bmp格式转换成jpg,却没有直接的方法,都是通过第三方包进行转换,
下面是参考网上的一篇文章略做了修改后的bmp转jpg的程序:

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 java.io.IOException;  
import com.sun.image.codec.jpeg.JPEGCodec;  
import com.sun.image.codec.jpeg.JPEGImageEncoder;  
public class BmpReader  
{  
     /**
      * Created on 2010-7-13  
      * Discription:[bmpTojpg]
      * @param file
      * @param dstFile
      */  
     public static void bmpTojpg(String file,String dstFile)  
     {  
         try  
         {  
             FileInputStream in = new FileInputStream(file);  
             Image TheImage = 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);  
         }  
     }  
     public static int constructInt(byte[] in, int offset)  
     {  
         int ret = ((int) in[offset + 3] & 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);  
     }  
     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);  
     }  
     public static long constructLong(byte[] in, int offset)  
     {  
         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);  
     }  
     public static double constructDouble(byte[] in, int offset)  
     {  
         long ret = constructLong(in, offset);  
         return (Double.longBitsToDouble(ret));  
     }  
     public static short constructShort(byte[] in, int offset)  
     {  
         short ret = (short) ((short) in[offset + 1] & 0xff);  
         ret = (short) ((ret << 8) | (short) ((short) in[offset + 0] & 0xff));  
         return (ret);  
     }  
static class BitmapHeader  
     {  
public int iSize,ibiSize,iWidth,iHeight,iPlanes,iBitcount,iCompression,iSizeimage,iXpm,iYpm,iClrused,iClrimp;  
         // 读取bmp文件头信息  
         public void read(FileInputStream fs) throws IOException  
         {  
             final int bflen = 14;  
             byte bf[] = new byte[bflen];  
             fs.read(bf, 0, bflen);  
             final int bilen = 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);  
             iCompression = constructInt(bi, 16);  
             iSizeimage = constructInt(bi, 20);  
             iXpm = constructInt(bi, 24);  
             iYpm = constructInt(bi, 28);  
             iClrused = constructInt(bi, 32);  
             iClrimp = constructInt(bi, 36);  
         }  
     }  
     public static Image read(FileInputStream fs)  
     {  
         try  
         {  
             BitmapHeader bh = new BitmapHeader();  
             bh.read(fs);  
             if (bh.iBitcount == 24)  
             {  
                 return (readImage24(fs, bh));  
             }  
             if (bh.iBitcount == 32)  
             {  
                 return (readImage32(fs, bh));  
             }  
             fs.close();  
         }  
         catch (IOException e)  
         {  
             System.out.println(e);  
         }  
         return (null);  
     }  
     //24位  
     protected static Image readImage24(FileInputStream fs, BitmapHeader bh) throws IOException  
     {  
         Image image;  
         if (bh.iSizeimage == 0)  
         {  
             bh.iSizeimage = ((((bh.iWidth * bh.iBitcount) + 31) & ~31) >> 3);  
             bh.iSizeimage *= bh.iHeight;  
         }  
         int npad = (bh.iSizeimage / bh.iHeight) - bh.iWidth * 3;  
         int ndata[] = new int[bh.iHeight * bh.iWidth];  
         byte brgb[] = new byte[(bh.iWidth + npad) * 3 * bh.iHeight];  
         fs.read(brgb, 0, (bh.iWidth + npad) * 3 * bh.iHeight);  
         int nindex = 0;  
         for (int j = 0; j < bh.iHeight; j++)  
         {  
             for (int i = 0; i < bh.iWidth; i++)  
             {  
                 ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3(brgb, nindex);  
                 nindex += 3;  
             }  
             nindex += npad;  
         }  
         image = Toolkit.getDefaultToolkit().createImage(  
                 new MemoryImageSource(bh.iWidth, bh.iHeight, ndata, 0, bh.iWidth));  
         fs.close();  
         return (image);  
     }  
     //32位  
     protected static Image readImage32(FileInputStream fs, BitmapHeader bh) throws IOException  
     {  
         Image image;  
         int ndata[] = new int[bh.iHeight * bh.iWidth];  
         byte brgb[] = new byte[bh.iWidth * 4 * bh.iHeight];  
         fs.read(brgb, 0, bh.iWidth * 4 * bh.iHeight);  
         int nindex = 0;  
         for (int j = 0; j < bh.iHeight; j++)  
         {  
             for (int i = 0; i < bh.iWidth; i++)  
             {  
                 ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3(brgb, nindex);  
                 nindex += 4;  
             }  
         }  
         image = Toolkit.getDefaultToolkit().createImage(  
                 new MemoryImageSource(bh.iWidth, bh.iHeight, ndata, 0, bh.iWidth));  
         fs.close();  
         return (image);  
     }  
     public static void main(String[] args)  
     {  
         String srcfile = "c:\\img\\bbb.bmp";  
         String dstFile = "c:\\img\\bbbimg.jpg";  
         bmpTojpg(srcfile,dstFile);  
     }
}
回复

使用道具 举报

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

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-6-7 16:58 , Processed in 0.387654 second(s), 46 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表