導航:首頁 > 文件處理 > 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圖片等比例壓縮相關的資料

熱點內容
mt伺服器怎麼下載迷你體驗服 瀏覽:367
新創建文件夾怎麼放置在文件夾中 瀏覽:777
嘉興電子文檔加密軟體供應商 瀏覽:139
華為手機怎麼加密視頻 瀏覽:763
湖南奇幻解壓管 瀏覽:671
蘋果平板中怎麼刪除查找app 瀏覽:303
linux配dns 瀏覽:350
仿生機器人編程 瀏覽:662
摳圖哪個app最好 瀏覽:327
玉林市公司程序員 瀏覽:106
單片機點陣顯示文字16 瀏覽:771
電腦怎麼對手機進行加密 瀏覽:719
手機遠程騰訊雲伺服器電腦安全嗎 瀏覽:283
怎麼在智慧屏上安裝米家app 瀏覽:37
焦慮解壓冥想師 瀏覽:166
re管理器怎麼提取u盤app 瀏覽:906
php和類名相同的方法 瀏覽:360
香港台灣dns伺服器地址列表 瀏覽:538
大同app怎麼樣 瀏覽:438
php去掉特殊字元 瀏覽:387