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要实现把一个大图片压缩到指定大小的图片且长宽比不变可以尝试以下操作: