資源簡介
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
評論
共有 條評論