讀取24位 BMP 影象並生成 JPG 縮圖(一)
阿新 • • 發佈:2019-01-02
if(nbitcount==24){
int npad=(nsizeimage/nheight)-nwidth*3;
int ndata[]=new int[nheight*nwidth];
byte brgb[]=new byte[(nwidth+npad)*3*nheight];
fs.read (brgb,0,(nwidth+npad)*3*nheight);
int nindex=0;
for(int j=0;j<nheight;j++){
for(int i=0;i<nwidth;i++){
ndata [nwidth*(nheight-j-1)+i]=
(255&0xff)<<24
| (((int)brgb[nindex+2]&0xff)<<16)
| (((int)brgb[nindex+1]&0xff)<<8)
| (int)brgb[nindex]&0xff;
nindex+=3;
}
nindex+=npad;
}
Toolkit kit=Toolkit.getDefaultToolkit();
image=kit.createImage(new MemoryImageSource(nwidth,nheight,
ndata,0,nwidth));
result="從BMP得到影象image";
System.out.println("從BMP得到影象image");
}else{
result="不是24位BMP,失敗!";
System.out.println("不是24位BMP,失敗!");
image=(Image)null;
}
fs.close(); //關閉輸入流
//開始進行影象壓縮(對image物件進行操作)
int wideth=image.getWidth(null); //得到源圖寬
int height=image.getHeight(null); //得到源圖長
BufferedImage tag=new BufferedImage(wideth/2,height/2,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(image,0,0,wideth/2,height/2,null); //繪製縮小後的圖
FileOutputStream out=new FileOutputStream("/newfile.jpg"); //輸出到檔案流
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //進行JPEG編碼
out.close(); //關閉輸出流
}catch (Exception e){
System.out.println(e);
}
return result;
}
}
(請注意!引用、轉貼本文應註明原作者:Rosen Jiang 以及出處:http://blog.csdn.net/rosen)
