图片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);
} }
图片bmp格式转换为jpg格式的更多相关文章
- elf格式转换为hex格式文件的两种方法
这周工作终于不太忙了,可以写点笔记总结一下了. 之前的文章如何在Keil-MDK开发环境生成Bin格式文件,介绍了如何在Keil开发环境使用fromelf软件,将生成的axf文件转换为bin文件,这次 ...
- 将windows文本格式转换为UNIX格式
将windows文本格式转换为UNIX格式 1.使用sed命令来进行转换,如下: sed -e ’s,^M,,g’ textfile 其中^M的输入方法是Ctrl+V, Ctrl+M 对于批量文件的处 ...
- webm视频转换 其他视频格式转换为webm格式
将其他视频格式转换为webm格式 https://files.cnblogs.com/files/bubuchu/html5videoshipingeshizhuanhuanqi.zip
- Sony索尼数码录音笔MSV格式转换为MP3格式【转】
本文转载自:http://blog.sina.com.cn/s/blog_4b2c860f0100d78w.html Sony索尼数码录音笔一般存储为WAV格式,有些没有特意修改存储格式的就保存为MS ...
- 如何将腾讯视频的qlv格式转换为mp4格式
基本上每个视频app都会有自己固有的视频播放格式,比如优酷的KUX.爱奇艺的QSV和腾讯的QLV等.而今天我们重点介绍腾讯的QLV格式如何转换为MP4格式,小便也是经过多次的摸索多次的软件试用,发现的 ...
- 图像格式转换之BMP格式转换为JPG格式
// bmp2jpg.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "jpeglib.h" #inc ...
- Photoshop教程,视频MP4格式转换为GIF格式
转自百度问题 https://zhidao.baidu.com/question/1497485136643778259.html Adobe PhotoShop软件的最bai新du本是可以编辑视zh ...
- 【程序练习】——ini格式转换为xml格式
;Configuration of http [http] doamin=www.mysite.com port= cgihome=/cgi-bin ;Configuration of db [d ...
- excel日期格式转换为文本格式
今天测试读取excel并修改数据库数据的时候遇到几个小问题. 1.空指针,读写io异常蛮多的,获取不到的数据就是null 2.读取文件位置,开始找不到文件 3.读取日期格式结果是一个数值,因此需要转化 ...
随机推荐
- 编译基于ARM LINUX的驱动模块的Makefile
KERNELDIR =/home/wenhao/platform/linux-2.6.34PWD := $(shell pwd)CROSS_COMPILE = /usr/local/arm/4.3.2 ...
- Java类初始化顺序问题
main -> (静态变量.静态代码块) ->main函数体 -> (类变量.初始化块.实例化引用的类) -> 构造函数 初始化块与实例化引用的类 的调用顺序 按程序的编写上下 ...
- UITableView基本使用和cell的属性
在ios的UI中UITableView是个常用且强大的控件 基本使用: 1>设置代理,一般把控制器设为代理:self.tableView.delegate = self; 2>遵守代理的协 ...
- TF-IDF与余弦相似性的应用(三):自动摘要
有时候,很简单的数学方法,就可以完成很复杂的任务. 这个系列的前两部分就是很好的例子.仅仅依靠统计词频,就能找出关键词和相似文章.虽然它们算不上效果最好的方法,但肯定是最简便易行的方法. 今天,依然继 ...
- 【Length of Last Word】cpp
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- 了解Git
对于计算机软件初学者来说Git并没有太多了解, 以前没有接触过,但是老师说对其进行了解,也没有什么概念,只有通过上网进行了解 . 了解到的大概内容如下: ...
- Linux - 常用命令行(一)
今天和大家分享一些最基本常用命令行:也是作为新手最应该了解掌握的 ls 命令:用来显示指定工作目录下内容 dir命令:与ls命令一致 cd 命令:变化工作目录 pwd命令:显示用户当前的工作路径,显 ...
- Mac上安装 mySql
今天在mac系统上安装了 mySql 和大家分享下 安装的过程.. 首先 第一步 需要在oracle的网站下载 mysql 的mac 版本. 下载地址如下: http://www.mysql.com/ ...
- HDU 2196 求树上所有点能到达的最远距离
其实我不是想做这道题的...只是今天考试考了一道类似的题...然后我挂了... 但是乱搞一下还是有80分....可惜没想到正解啊! 所以今天的考试题是: 巡访 (path.pas/c/cpp) Cha ...
- 【BZOJ】【1034】【ZJOI2008】泡泡堂BNB
贪心 类似田忌赛马策略的一个贪心= = 随便YY了一个做法居然A了…… 简单来说就是先强对强,弱对弱,能赢就赢,不能赢就让弱的那个去对强的那个,剩下的人继续依次捉对比赛(继续刚刚的策略),现在人数还是 ...