资源简介
java实现图片bmp转换压缩为jpg,win7格式下转换后图片和原图看起来差别不大
代码片段和文件信息
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 heightBufferedImage.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
评论
共有 条评论