欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Java圖片壓縮代碼,java實(shí)現(xiàn)圖片壓縮

java如何實(shí)現(xiàn)把一個(gè)大圖片壓縮到指定大小的圖片且長寬比不變

也就是圖片壓縮,可以不修改任何大小的壓縮(速度快),也可等比例修改大小壓縮(較慢)

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比榕城網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式榕城網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋榕城地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。

下面這是一段等比例縮小圖片的方法。

public String compressPic(String inputDir, String outputDir,

String inputFileName, String outputFileName, int width,

int height, boolean gp,String hzm) {

try {

if (!image.exists()) {

return "";

}

Image img = ImageIO.read(image);

// 判斷圖片格式是否正確

if (img.getWidth(null) == -1) {

return "no";

} else {

int newWidth; int newHeight;

// 判斷是否是等比縮放

if (gp == true) {

// 為等比縮放計(jì)算輸出的圖片寬度及高度

double rate1 = ((double) img.getWidth(null)) / (double) width ;

double rate2 = ((double) img.getHeight(null)) / (double) height ;

// 根據(jù)縮放比率大的進(jìn)行縮放控制

double rate = rate1 rate2 ? rate1 : rate2;

newWidth = (int) (((double) img.getWidth(null)) / rate);

newHeight = (int) (((double) img.getHeight(null)) / rate);

} else {

newWidth = img.getWidth(null); // 輸出的圖片寬度

newHeight = img.getHeight(null); // 輸出的圖片高度

}

BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);

/*

* Image.SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的

* 優(yōu)先級比速度高 生成的圖片質(zhì)量比較好 但速度慢

*/

Image im = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);

tag.getGraphics().drawImage(im, 0, 0, null);

FileOutputStream out = new FileOutputStream(outputDir + outputFileName);

//JPEGImageEncoder可適用于其他圖片類型的轉(zhuǎn)換

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag);

out.close();

}

} catch (IOException ex) {

ex.printStackTrace();

}

return "ok";

}

如何用java 調(diào)整jepg圖片壓縮

ImageWriter writer; // 自己獲取 ImageWriter 對象

ImageWriteParam iwp = writer.getDefaultWriteParam();

iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);

// 參數(shù)為0和1

// 1表示設(shè)置最小的壓縮以保持最大的圖片質(zhì)量

iwp.setCompressionQuality(1);

File file = new File(OUTPUTFILE);

FileImageOutputStream output = new FileImageOutputStream(file);

writer.setOutput(output);

IIOImage image = new IIOImage(BUFFEREDIMAGE, null, null);

// 寫入圖片

writer.write(null, image, iwp);

writer.dispose()

問了一下我遠(yuǎn)標(biāo)教育的哥們,希望對你有用!

求助java壓縮圖片存儲大小的方法

可以使用Draw這個(gè)類,通過改變像素來改變存儲大小,實(shí)例如下:

public?static?boolean?compressPic(String?srcFilePath,?String?descFilePath)?throws?IOException?{

File?file?=?null;

BufferedImage?src?=?null;

FileOutputStream?out?=?null;

ImageWriter?imgWrier;

ImageWriteParam?imgWriteParams;

//?指定寫圖片的方式為?jpg

imgWrier?=?ImageIO.getImageWritersByFormatName("jpg").next();

imgWriteParams?=?new?javax.imageio.plugins.jpeg.JPEGImageWriteParam(

null);

//?要使用壓縮,必須指定壓縮方式為MODE_EXPLICIT

imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);

//?這里指定壓縮的程度,參數(shù)qality是取值0~1范圍內(nèi),

imgWriteParams.setCompressionQuality((float)?1);

imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);

ColorModel?colorModel?=ImageIO.read(new?File(srcFilePath)).getColorModel();//?ColorModel.getRGBdefault();

//?指定壓縮時(shí)使用的色彩模式

//????????imgWriteParams.setDestinationType(new?javax.imageio.ImageTypeSpecifier(

//????????????????colorModel,?colorModel.createCompatibleSampleModel(16,?16)));

imgWriteParams.setDestinationType(new?javax.imageio.ImageTypeSpecifier(

colorModel,?colorModel.createCompatibleSampleModel(16,?16)));

try?{

if?(isBlank(srcFilePath))?{

return?false;

}?else?{

file?=?new?File(srcFilePath);System.out.println(file.length());

src?=?ImageIO.read(file);

out?=?new?FileOutputStream(descFilePath);

imgWrier.reset();

//?必須先指定?out值,才能調(diào)用write方法,?ImageOutputStream可以通過任何

//?OutputStream構(gòu)造

imgWrier.setOutput(ImageIO.createImageOutputStream(out));

//?調(diào)用write方法,就可以向輸入流寫圖片

imgWrier.write(null,?new?IIOImage(src,?null,?null),

imgWriteParams);

out.flush();

out.close();

}

}?catch?(Exception?e)?{

e.printStackTrace();

return?false;

}

return?true;

}

public?static?boolean?isBlank(String?string)?{

if?(string?==?null?||?string.length()?==?0?||?string.trim().equals(""))?{

return?true;

}

return?false;

}

當(dāng)前標(biāo)題:Java圖片壓縮代碼,java實(shí)現(xiàn)圖片壓縮
文章網(wǎng)址:http://chinadenli.net/article46/dseiheg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣自適應(yīng)網(wǎng)站ChatGPT定制網(wǎng)站軟件開發(fā)標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)