图片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.读取日期格式结果是一个数值,因此需要转化 ...
随机推荐
- 各设备如何清理dns缓存
Windows 按下 Windows+R 键,运行 cmd ,在命令提示符运行命令 ipconfig /flushdns OS X 10.10 在[应用程序][实用工具][终端]运行命令 sudo d ...
- 解决sharepoint 2010 用户配置文件同步服务 正在启动
用户配置文件同步服务一直显示“正在启动”,而且无法停止,如下办法可以停止这个服务: 在sharepoint power shell 中执行下面的命令: Get-spserviceinstance 获取 ...
- Qt入门1---widget、mainwindow和Dialog区别
摘要: 看了一个月的Qt,居然没有理清Qt中 ------------------------------------ 1.QMainWindow A main window provides a f ...
- 30.DDR2问题2_local_init_done为什么没拉高?
按照初始化时序,在200us时,mem_clk时钟稳定,开始初始化设置,设置完后,会产生一个初始化完成标志,local_init_done会拉高,没有拉高,可能有以下几个原因: 1.确认DDR2 IP ...
- Android实现SharePreferences和AutoCompletedTextView
Android实现SharePreferences和AutoCompletedTextView 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 ...
- 如何向VS2010中插入ActiveX控件并且附带相应的类
上两篇文章中我们已经讲述了ActiveX控件的一些相关知识,本文中,简单说明一下如何在我们自己的程序中使用ActiveX控件.(仍以我们上节课的例子为例) 我们打开VS2010编辑器,新建一个基于对话 ...
- 查看BADI有哪些实现
TCODE:SE18
- Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. 参考:http://standalone.iteye.com/b ...
- C# 微信公众号
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 20145120 《Java程序设计》第7周学习总结
20145120 <Java程序设计>第7周学习总结 教材学习内容总结 Lambda表达式 例:Comparator<String> byLength = (name1, na ...