图片bmp格式转换为jpg格式

一下代码经过个人测试,可用

注意:将jpg格式的图片重命名为bmp格式,在该代码中是不能转换的,会报空值异常!而且IE10是显示不了这样的图片的

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 {

    /**
     * 图片格式转换 BMP -> JPG
     * @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 = "D:\\33.bmp";
        String dstFile = "D:\\33.jpg";
        bmpTojpg(srcfile, dstFile);
    }

}
时间: 2024-08-01 23:11:11

图片bmp格式转换为jpg格式的相关文章

kvm中raw格式转换为qcow2格式节省主机空间

kvm中raw格式转换为qcow2格式节省主机空间 virsh list --all qemu-img  info /data2/fdfs1.img qemu-img convert -f raw -O qcow2 /data2/CDH_003.img /data2/CDH_003.qcow2 virsh edit CDH_003 chown -R   qemu:qemu /data2/CDH_003.qcow2 然后通过vnc图形化界面连接对应的宿主IP,把如下地方打钩 原文地址:http:/

超星pdg格式转换为pdf格式原理方法

http://www.zaotangzi.net/Documents/12868/1286801.html 超星的图书量非常多,不过我们下载到的超星图书都是pdg格式,而且文件都是单列的,一个文件夹下几百个文件,看起来很不舒服,所以把每本超星图书转换为单独的pdf格式,就会美观大方很多了.原理:和Word,PPT等转换为PDF格式的原理相似,安装PDF虚拟打印机驱动,然后在超星浏览器中选择PDF虚拟打印机,然后指定存放的目的地和文件名就可以了. 事实上很多软件都能实现PDF虚拟打印机的功能,所以

图像格式转换之BMP格式转换为JPG格式

1 // bmp2jpg.cpp : 定义控制台应用程序的入口点. 2 // 3 #include "stdafx.h" 4 #include "jpeglib.h" 5 #include "stdlib.h" 6 #pragma comment(lib,"libjpeg.lib") 7 #pragma pack(4) //两字节对齐,否则bmp_fileheader会占16Byte 8 9 /*图像bmp格式文件头结构*/

使用editcap命令将ERF格式转换为pcap格式

editcap是Wireshark的一个组件,在Windows平台下,只要完成Wireshark的安装,就可以在安装目录中看到editcap.exe.editcap.exe需要在命令行中使用. 对于用Endace DAG捕捉卡捕获的数据包,一般来说,都是erf格式的.ERF格式全称是Extensible Record Format,具体格式参见http://wiki.wireshark.org/ERF.可以看到,这和pcap文件格式是完全不同的,一般来说,ERF格式的文件包含更多的链路层的信息.

【转】将 azw3 格式转换为 mobi 格式并保持原有排版格式

小伙伴多次向 Kindle 伴侣提出一个问题,那就是通过 Calibre 将排版精美的 azw3 格式电子书转换成 mobi 格式后推送到 Kindle,排版格式会发生很大的变化,比如行距过窄.内嵌字体丢失等等,有没有方法避免这种情况呢?答案当然是肯定的. 通过实测发现,先用 Calibre 将 azw3 格式电子书转换成 epub 格式,然后再用 Kindlegen 把 epub 格式转换成 mobi 格式,这样一来,转换后的 mobi 格式电子书将原汁原味地保持原 azw3 格式的排版风格.

excel日期格式转换为文本格式

今天测试读取excel并修改数据库数据的时候遇到几个小问题. 1.空指针,读写io异常蛮多的,获取不到的数据就是null 2.读取文件位置,开始找不到文件 3.读取日期格式结果是一个数值,因此需要转化为文本,方法如下: 首先,函数TEXT(单元格,'format') ,比如=TEXT(A2,'yyy-mm-dd'),然后enter,然后拉到整列. 然后,选中复制,选择性粘贴,选择数值. 刚做完就读取,空指针,于是处理内容为空的数据.

CST 时间格式转换为Date格式

CST格式:Tue Jul 31 11:37:41 CST 2012 Date格式:2016-08-04 02:06:20 1.在页面进入该标签: <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 2.value参数就是从后台取到的时间: 对输入的时间添加: <fmt:formatDate value="${dateTime}" pattern=&

将磁盘从FAT格式转换为NTFS格式的方法

不需要进行格式化,只需在命令提示符中输入如下内容:CONVERT X:/FS:NTFS把X换成你需要的盘符,转一个盘需十几或几十秒不等..注意:此方法不可逆转,FAT32转到NTFS后不可转回,当然也没必要转回,一定要转回可以格式化硬盘.

json日期格式转换为正常格式

function jsonDateFormat(jsonDate) { try { var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.g