package com.foxconn.service.util;
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.JPEGImageEncoder;
/*
* 將JDK不支持的BMP格式轉換成支持的JPG格式
* @author LYJ
* @date 2010/09/29
*/
public class ESBMPImg2PIGUtil {
/**
* 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 = "d:\\aa.bmp";
String dstFile = "d:\\aa1.jpg";
bmpTojpg(srcfile,dstFile);
}
}
分享到:
相关推荐
因为jdk只识别jpg、png、gif这三种格式的在使用java的时候会需要将bmp格式的图片文件转换成jpg、png、gif等格式,使用此代码可将bmp格式转换成jpg格式,能否转换成其他格式的文件还没有测试
能将体积庞大的bmp格式图片压缩转换为小巧的JPG图片,也可以转换其他常见的图片格式。同时还能自由设定压缩比率。
此程序实现10位的CCD纯数据格式raw文件转换为8位的bmp格式文件,使用时需改变其中的H、W为自己图片的高和宽。
java程序,读取BMP图片的数据存到数组中,利用java类的压缩算法,将BMP图片转换为JPG格式的图片。
java实现图片bmp转换压缩为jpg,win7格式下转换后图片和原图看起来差别不大
使用OPENCV库,将YUYV格式的视频转换为BMP格式的图片并保存,在VS2017环境下,亲测可用
android jni 将wlt图片转成bmp格式图片,支持32位armeabi架构手机
Bmp2raw is a tool to convert 24bit BMP file to raw data. It may be useful before generating C arrays for showing a picture in a embeded system. The color order in the output file is (B,G,R),(B,G,R),...
Android不支持将Bitmap转换成单色的Bmp图片,所以参考Bmp格式说明,自己写了一个转换类。亲测有效!!!
BMP图像格式数据的读入函数及其计算机显示毕业论文,全套论文,程序源码,外文翻译,程序截图,答辩ppt
根据图片内容判断图片的格式、 把bmp格式的图片转换成jpg格式
JAVA图片类型转换 实例. 如果需要在java中将 bmp 的图片转成 gif 格式的,可以参考一下这个例子。
银联电子签名,jbig压缩格式转为可见格式(如bmp,jpg等)。使用方式如下 public static void main(String[] args) { String data="0000010000000140000000AA000000070800031CFF02FF02C2347F2D42C9D48BEBB224FF...
argb1555转bmp图片
java依赖jai-imageio,非常方便地实现jpg图片转为raw格式图片,raw格式图片转为jpg图片
用JAVA 写代码,实现把图片从BMP格式转换到JPG格式
java图片格式转换.特别实现其它格式转到ico,例如原图是bmp转到.ico亲自编写测试可用.生成后的图片可以自由指定保存路径,默认和src同一级目录
java写的图片格式转换程序,支持jpg,bmp等格式
把JPEG图片生成单色(黑白色)BMP图片。
实现思路: 1、获取图片的二值化字节数组 这一步是关键 2、将字节数组转为十六进制 3、压缩十六进制字符串 结尾为1、0或者与上一行相同的;相同的连续字符压缩 4、拼凑ZPL编码,宽度需要扩大,因为需要时8个点(1字节)...