1、bmp转jpg
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.io.File;
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 BmpToJpgUtil {
public static void bmpTojpg(File file,FiledstFile)
{
try
{
FileInputStream in = newFileInputStream(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 = newFileOutputStream(dstFile);
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}catch(Exception e)
{
e.printStackTrace();
System.out.println(e.getMessage());
}
}
public static int constructInt(byte[] in, intoffset)
{
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, intoffset)
{
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, intoffset)
{
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, intoffset)
{
long ret = constructLong(in, offset);
return (Double.longBitsToDouble(ret));
}
public static short constructShort(byte[] in, intoffset)
{
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) throwsIOException
{
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(FileInputStreamfs)
{
try
{
BitmapHeader bh = new BitmapHeader();
bh.read(fs);
switch(bh.iBitcount){
case 1: {return (readImage1(fs, bh));}
case 4: {return (readImage4(fs, bh));}
case 8: {return (readImage8(fs, bh));}
case 16: {return (readImage16(fs, bh));}
case 24: {return (readImage24(fs, bh));}
case 32: {return (readImage32(fs, bh));}
default:{return null;}
}
}
catch (IOException e)
{
System.out.println(e);
}
return (null);
}
//1位
protected static Image readImage1(FileInputStream fs, BitmapHeaderbh) throws IOException{
// Have to determine the number of colors, theclrsused
// parameter is dominant if it is greater thanzero. If
// zero1, calculate colors based on bitsperpixel.
Image image;
int nNumColors = 0;
if (bh.iClrused > 0) {
nNumColors = bh.iClrused;
}
else {
nNumColors = (1 & 0xff) << bh.iBitcount;
}
System.out.println("The number of Colors is" + nNumColors);
// Read the palatte colors.
int npalette[] = new int[nNumColors];
byte bpalette[] = new byte[nNumColors * 4];
fs.read(bpalette, 0, nNumColors * 4);
int nindex1 = 0;
for (int n = 0; n < nNumColors; n++) {
npalette[n] = (255 & 0xff) << 24
| ( ( (int) bpalette[nindex1 + 2] & 0xff) << 16)
| ( ( (int) bpalette[nindex1 + 1] & 0xff) << 8)
| (int) bpalette[nindex1] & 0xff;
nindex1 += 4;
}
// Scan line is padded with zeroes to be a multiple of fourbytes
int scanLineSize = ( ( (bh.iWidth * bh.iBitcount) + 31) & ~31)>> 3;
int ndata1[] = new int[bh.iWidth * bh.iHeight];
//byte bdata[] = new byte[ scanLineSize * nheight];
byte blinedata[] = new byte[scanLineSize];
//fs.read(bdata, 0, scanLineSize * nheight);
nindex1 = 0;
for (int j1 = 0; j1 < bh.iHeight; j1++) {
fs.read(blinedata, 0, scanLineSize);
nindex1 = 0;
for (int i1 = 0; i1 < bh.iWidth; i1++) {
if (bh.iWidth * (bh.iHeight - j1 - 1) + i1 > bh.iWidth *bh.iHeight-1)
break;
if (nindex1 > scanLineSize * bh.iHeight - 1)
break;
for (int pixPerByte = 0; pixPerByte < 8; pixPerByte++)
{
int shift = 8 - pixPerByte - 1;
ndata1[bh.iWidth * (bh.iHeight - j1 - 1) + i1] =
npalette[ ( (int) (blinedata[nindex1]>>shift) &0x1)];
if (pixPerByte != 7)
{
i1++;
if (i1 >= bh.iWidth)
break;
}
}
nindex1++;
}
}
image = Toolkit.getDefaultToolkit().createImage
(new MemoryImageSource(bh.iWidth, bh.iHeight,
ndata1, 0, bh.iWidth));
fs.close();
return image;
}
//4位
protected static Image readImage4(FileInputStream fs, BitmapHeaderbh) throws IOException{
// Have to determine the number of colors, theclrsused
// parameter is dominant if it is greater thanzero. If
// zero1, calculate colors based on bitsperpixel.
Image image;
int nNumColors = 0;
if (bh.iClrused > 0) {
nNumColors = bh.iClrused;
}
else {
nNumColors = (1 & 0xff) << bh.iBitcount ;
}
System.out.println("The number of Colors is" + nNumColors);
// Read the palatte colors.
int npalette[] = new int[nNumColors];
byte bpalette[] = new byte[nNumColors * 4];
fs.read(bpalette, 0, nNumColors * 4);
int nindex4 = 0;
for (int n = 0; n < nNumColors; n++) {
npalette[n] = (255 & 0xff) << 24
| ( ( (int) bpalette[nindex4 + 2] & 0xff) << 16)
| ( ( (int) bpalette[nindex4 + 1] & 0xff) << 8)
| (int) bpalette[nindex4] & 0xff;
nindex4 += 4;
}
// Scan line is padded with zeroes to be a multiple of fourbytes
int scanLineSize = ( ( (bh.iWidth * bh.iBitcount) + 31) & ~31)>> 3;
// Read the image data (actually indices into the palette)
// Scan lines are still padded out to even 4-byte
// boundaries.
//int npad4 = (nsizeimage / nheight) - nwidth / 2;
int ndata4[] = new int[bh.iWidth * bh.iHeight];
//byte bdata[] = new byte[ scanLineSize * nheight];
byte blinedata[] = new byte[scanLineSize];
//fs.read(bdata, 0, scanLineSize * nheight);
nindex4 = 0;
for (int j4 = 0; j4 < bh.iHeight; j4++){
fs.read(blinedata, 0, scanLineSize);
nindex4 = 0;
for (int i4 = 0; i4 < bh.iWidth; i4++) {
if (bh.iWidth * ( bh.iHeight - j4 - 1) + i4 > bh.iWidth* bh.iHeight-1)
break;
if (nindex4 > scanLineSize * bh.iHeight -1)
break;
for (int pixPerByte = 0; pixPerByte < 2; pixPerByte++)
{
if (pixPerByte == 0)
{
ndata4[bh.iWidth * ( bh.iHeight - j4 - 1) + i4] =
npalette[ ( (int) (blinedata[nindex4]>>4) & 0xf)];
i4++;
if (i4 >= bh.iWidth)
break;
}
else
{
ndata4[bh.iWidth * ( bh.iHeight - j4 - 1) + i4] =
npalette[ ( (int) blinedata[nindex4] & 0xf)];
}
}
nindex4++;
}
}
image = Toolkit.getDefaultToolkit().createImage
(new MemoryImageSource(bh.iWidth, bh.iHeight,
ndata4, 0, bh.iWidth));
fs.close();
return image;
}
//8位
protected static Image readImage8(FileInputStream fs, BitmapHeaderbh) throws IOException{
// Have to determine the number of colors, theclrsused
// parameter is dominant if it is greater thanzero. If
// zero1, calculate colors based on bitsperpixel.
Image image;
int nNumColors = 0;
if (bh.iClrused> 0) {
nNumColors = bh.iClrused;
}
else {
nNumColors = (1 & 0xff) << bh.iBitcount;
}
System.out.println("The number of Colors is" + nNumColors);
// Read the palatte colors.
int npalette[] = new int[nNumColors];
byte bpalette[] = new byte[nNumColors * 4];
fs.read(bpalette, 0, nNumColors * 4);
int nindex8 = 0;
for (int n = 0; n < nNumColors; n++) {
npalette[n] = (255 & 0xff) << 24
| ( ( (int) bpalette[nindex8 + 2] & 0xff) << 16)
| ( ( (int) bpalette[nindex8 + 1] & 0xff) << 8)
| (int) bpalette[nindex8] & 0xff;
nindex8 += 4;
}
// Read the image data (actually indices into the palette)
// Scan lines are still padded out to even 4-byte
// boundaries.
int npad8 = (bh.iSizeimage / bh.iHeight) - bh.iWidth;
System.out.println("nPad is:" + npad8);
int ndata8[] = new int[bh.iWidth * bh.iHeight];
byte bdata[] = new byte[ (bh.iWidth + npad8) * bh.iHeight];
fs.read(bdata, 0, (bh.iWidth + npad8) * bh.iHeight);
nindex8 = 0;
for (int j8 = 0; j8 < bh.iHeight; j8++) {
for (int i8 = 0; i8 < bh.iWidth; i8++) {
ndata8[bh.iWidth * (bh.iHeight - j8 - 1) + i8] =
npalette[ ( (int) bdata[nindex8] & 0xff)];
nindex8++;
}
nindex8 += npad8;
}
image = Toolkit.getDefaultToolkit().createImage
(new MemoryImageSource(bh.iWidth, bh.iHeight,
ndata8, 0, bh.iWidth));
fs.close();
return image;
}
//16位
protected static Image readImage16(FileInputStream fs, BitmapHeaderbh) throws IOException{
Image image;
// int npad = (nsizeimage / nheight) - nwidth *2;
// int ndata[] = new int[nheight * nwidth];
// bytebrgb[] = new byte[ (nwidth + npad) * 2 * nheight];
int npad = (bh.iSizeimage / bh.iHeight) -bh.iWidth * 2;
int ndata[] = new int[bh.iHeight *bh.iWidth];
byte brgb[] = new byte[bh.iWidth * 2 * bh.iHeight];
fs.read(brgb, 0, (bh.iWidth + npad) * 2 * 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] =
(255 & 0xff) << 24
| ( ( ( ((int)brgb[nindex + 1] >>> 2) & 0x3f) |0x60)<<3 << 16)
| ( ( ( (int) (((brgb[nindex + 1]&0x3)<<3) |((brgb[nindex]&0xe0)>>>5))) | 0x60)<<3 <<8)
| ( ( ( (int) brgb[nindex] & 0x1f) | 0x60)<<3);
nindex += 2;
}
nindex += npad;
}
image = Toolkit.getDefaultToolkit().createImage
(new MemoryImageSource(bh.iWidth, bh.iHeight,
ndata, 0, bh.iWidth));
fs.close();
return image;
}
//24位
protected static Image readImage24(FileInputStream fs, BitmapHeaderbh) 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, BitmapHeaderbh) 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);
}
}