❶ 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);
}
}
}
❷ 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);//縮小
❸ 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(); //刷新屏幕
}
❹ java是怎麼實現等比例縮小圖片而不失真的啊
按照長寬的比例等比縮小,圖片最好用矢量圖片,不容易失真。。。
❺ java 如何取得圖片的寬度,厘米而非象素
建議使用PS軟體進行調整。
操作步驟:
1.打開需要處理的照片,在「圖像」中找到「圖像大小」。
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);
}