图片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.读取日期格式结果是一个数值,因此需要转化 ...
随机推荐
- ListBox mvvm 学习笔记
1. ListBox MvvM 例子1. 简单的绑定,ItemsSource 绑定到一个实现了IEnumerable 的类上.一般该绑定都是双向的,所以优先考虑使用 ObservableCollec ...
- ABAP后台JOB数量控制
数据库视图:V_OP 可以查看JOB信息 FORM sub_check_job. * 通过JOB名称,控制活动JOB的数量 , jobname TYPE btcjob , strtdate TYPE ...
- java 图片处理
/* * 图片处理类 */ package image; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.j ...
- 30道小学生四则运算题C/C++编程
软件工程科课上,老师通过实例讲解什么是程序,程序和软件的区别,要求我们通过短时间写一道编程题, 题目就是编写30道小学生四则运算题.以下就是源代码: #include<iostream.h> ...
- ios-UIWebView中js和oc代码的互调
webview是ios中显示远程数据的网页控件,webview能显示的内容很多,MP4.文本.pdf等等: 关于js和oc代码的互相调用 1:oc中调用js代码; >>oc中调用js代码很 ...
- C#加密解密算法汇总(转)
方法一: //须添加对System.Web的引用 using System.Web.Security; ... /// <summary& ...
- 本地wordpress博客系统安装搭建实践
我们按步骤来, (1)安装XAMPP集成软件包 wordpress 的运行要求是在 php + MySQL + Apache的服务器环境,所以要先搭建该环境,我用的是XAMPP软件包,安装很方便. 下 ...
- 关于ThreadLocal
ThreadLocal是用于并发环境下避免竞争,简化编程的机制,它在并发环境下提供了一个逻辑上全局的访问点,来访问线程本地对象. 其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个T ...
- Javascript使用function创建类的两种方法
1.使用function类 //myFunction.js var CMyFunc=function() { //类的公共方法,供外部调用 this.Func1=function() { var i= ...
- bzoj 1270 DP
w[i,j]代表高度j,第i颗树的时候的最大值 那么w[i,j]:=max(w[i,j+1],w[k,j+heigh])+sum[i,j]: 但是这样枚举是n^3的,我们发现转移的第二个选择w[k,j ...