導航:首頁 > 文件處理 > java圖片等比例壓縮

java圖片等比例壓縮

發布時間:2023-03-12 14:44:12

1. java將圖片按比例縮小

Image srcImg = ImageIO.read(new FileInputStream(fnSrc) );//取源圖
int width = 600; //假設要縮小到600點像素
int height = srcImg.getHeight(null)*600/srcImg.getWidth(null);//按比例,將高度縮減

System.out.println("Width: "+srcImg.getWidth(null));// 這幾行是調試用
System.out.println("Height: "+srcImg.getHeight(null));
System.out.println("Width2: "+width);
System.out.println("Height2: "+height);

Image smallImg =srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH);//縮小

2. Java 按比例縮小圖片應該怎麼實現

public Image resizeImg(Image img, int newWidth, int newHeight) {
int imgW = img.getWidth();
int imgH = img.getHeight();
int[] srcPixels = new int[imgW * imgH];
int[] destPixels = new int[newWidth * newHeight];
img.getRGB(srcPixels, 0, imgW, 0, 0, imgW, imgH);
int srcX;
int srcY;
for (int y = 0; y < newHeight; ++y) {
for (int x = 0; x < newWidth; ++x) {
srcX = (x * imgW) / newWidth;
srcY = (y * imgH) / newHeight;
destPixels[x + y * newWidth] = srcPixels[srcX + srcY * imgW];
}
}
return Image.createRGBImage(destPixels, newWidth, newHeight, true);
}

3. java怎麼實現通過滑鼠滾輪對圖片進行等比例縮放功能

給你個實例吧
public class Anniu extends JFrame implements MouseWheelListener {

private static final long serialVersionUID = 1L;

JLabel jl = null;
ImageIcon image = new ImageIcon("F://12.jpg");
public Anniu() {

}

public void init() {
setLayout(null);

jl = new JLabel(image);
setSize(800, 600);
jl.setBounds(300, 200, 200, 200);
add(jl);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseWheelListener(this);
setVisible(true);
}

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
// TODO Auto-generated method stub
int num = e.getWheelRotation();
int x = (this.getWidth() - jl.getWidth()) / 2;
int y = (this.getHeight() - jl.getHeight()) / 2;
int width=jl.getWidth()+ 2 * num;
int height=jl.getHeight()+ 2 * num;
jl.setIcon(new ImageIcon( image.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH))); //重點1:按所給的大小來設置圖片,不管圖片有多大,總是全圖顯示,可能導致失真!你可以加一個大小限制,超過圖片大小了則不再放大。
jl.setBounds(x + num, y + num, width, height);
this.repaint(); //刷新屏幕
}

4. java是怎麼實現等比例縮小圖片而不失真的啊

按照長寬的比例等比縮小,圖片最好用矢量圖片,不容易失真。。。

5. Java怎麼實現上傳圖片的等比縮放,原文件和縮放的文件都要上傳到伺服器

/**
*等比例縮放圖片
*@paraminfile
*@paramoutfile
*@paramwidth
*@paramheight
*@paramquality
*@throwsIOException
*@throwsInterruptedException
*/
publicstaticvoidThumbnail(Stringinfile,Stringoutfile,intwidth,intheight,intquality)throwsIOException,InterruptedException{
//savethumbnailimagetoOUTFILE
//System.out.println("infile:"+infile);
BufferedImagethumbImage=null;
BufferedOutputStreamout=null;
Imageimage=null;
image=Toolkit.getDefaultToolkit().createImage(infile);
MediaTrackermediaTracker=newMediaTracker(newContainer());
mediaTracker.addImage(image,0);
mediaTracker.waitForID(0);
intthumbWidth=width;
intthumbHeight=height;
doublethumbRatio=(double)thumbWidth/(double)thumbHeight;
intimageWidth=image.getWidth(null);
intimageHeight=image.getHeight(null);
doubleimageRatio=(double)imageWidth/(double)imageHeight;
if(thumbRatio<imageRatio){
thumbHeight=(int)(thumbWidth/imageRatio);
}else{
thumbWidth=(int)(thumbHeight*imageRatio);
}
thumbImage=newBufferedImage(thumbWidth,thumbHeight,BufferedImage.TYPE_INT_RGB);
Graphics2Dgraphics2D=thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image,0,0,thumbWidth,thumbHeight,null);
out=newBufferedOutputStream(newFileOutputStream(outfile));
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParamparam=encoder.getDefaultJPEGEncodeParam(thumbImage);
quality=Math.max(0,Math.min(quality,100));
param.setQuality((float)quality/100.0f,false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
out.close();
thumbImage=null;
out=null;
image=null;
}


原文轉載自:http://blog.chinaunix.net/uid-29337460-id-5035058.html

6. java如何實現把一個大圖片壓縮到指定大小的圖片且長寬比不變

java要實現把一個大圖片壓縮到指定大小的圖片且長寬比不變可以嘗試以下操作:

7. java 怎麼把gif圖按比例縮放

importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.awt.geom.*;
importjava.util.*;
finalclasscatextendsJFrame{
=newDimension(800,600);

publicStringgetTitle(){return"cat";}

(){returnSIZE;}

publicDimensiongetMinimumSize(){returnSIZE;}

publicDimensiongetMaximumSize(){returnSIZE;}

publicDimensiongetSize(){returnSIZE;}

privateCanvascanvas;
privateBrushbrush;

cat()throwsHeadlessException{
init();
attachListeners();
doLay();
setVisible(true);
}

privatevoidinit(){
brush=newCatBrush();
canvas=newCanvas(brush);
}

privatevoidattachListeners(){
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(1);
}
});
}

privatevoiddoLay(){
Containercontainer=getContentPane();
container.add(canvas,BorderLayout.CENTER);
pack();
}

publicstaticvoidmain(String...args){
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(cat::new);
}

{
/**
*dopaintaction
*@
*/
voidpaint(Graphicsg);

/**
*Thecomponentthatthisbrushhold
*@returncomponentinstance
*/
ComponentgetOwner();

voidsetOwner(Componentowner);
}
{
privateComponentowner;
privateImageimg;
=AffineTransform.getScaleInstance(.4d,.4d);
publicCatBrush(){
img=Toolkit.getDefaultToolkit().getImage("G:\cat.jpg");
}

publicComponentgetOwner(){
returnowner;
}

publicvoidsetOwner(Componentowner){
this.owner=owner;
}

publicvoidpaint(Graphicsg){
Graphics2Dg2=(Graphics2D)g.create();
g2.drawImage(img,affineTransform,getOwner());
g2.dispose();
}

publicvoipdate(Observableo,Objectarg){
if(owner!=null)owner.repaint();
}
}
/**
*youcanvas
*/
{
privateBrushbrush;

privateCanvas(Brushbrush){
super();
this.brush=brush;
brush.setOwner(this);
}

protectedvoidpaintComponent(Graphicsg){
super.paintComponent(g);
if(brush==null)return;
brush.paint(g);
}
}
}

8. java如何實現把一個大圖片壓縮到指定大小的圖片且長寬比不變

java要實現把一個大圖片壓縮到指定大小的圖片且長寬比不變可以嘗試以下操作:

閱讀全文

與java圖片等比例壓縮相關的資料

熱點內容
命令行參考 瀏覽:279
怎麼初步認識編程 瀏覽:208
為什麼程序員都喜歡谷歌 瀏覽:891
壓縮性骨拆能自愈嗎 瀏覽:277
安卓怎麼設置游戲畫面 瀏覽:114
k線上寫字源碼 瀏覽:457
單擊按鈕保存資料源碼 瀏覽:354
華為gt加密卡 瀏覽:213
河北超融合伺服器廠家雲主機 瀏覽:894
芙兒優安全座椅app怎麼連接 瀏覽:294
專業美團騎手app怎麼開通 瀏覽:949
個人音樂分享網站源碼 瀏覽:375
在新電腦上怎麼注冊加密狗 瀏覽:123
最後一戰游戲源碼 瀏覽:5
phpmysql實例下載 瀏覽:751
傳智黑馬安卓非加密 瀏覽:553
伺服器如何配置host 瀏覽:1001
守望執行命令 瀏覽:371
加密狗插上去了怎麼辦 瀏覽:624
錘子m1怎麼把文件夾重置 瀏覽:213