java代码缩小照片 java对图片进行缩放
怎么用java代码放大或缩小图片不失真。
放大图像不会导致失真,而缩小图像将不可避免的失真。Java中也同样是这样。但java提供了4个缩放的微调选项。image.SCALE_SMOOTH //平滑优先image.SCALE_FAST//速度优先image.SCALE_AREA_AVERAGING //区域均值image.SCALE_REPLICATE //像素复制型缩放image.SCALE_DEFAULT //默认缩放模式调用方法Image new_img=old_img.getScaledInstance(1024, 768, Image.SCALE_SMOOTH);得到一张缩放后的新图。怎么用java代码放大或缩小图片不失真。
成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、成都网站设计、大田网络推广、微信平台小程序开发、大田网络营销、大田企业策划、大田品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供大田建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com
java程序中如何设置图片大小?
用Image中的getScaledInstance方法得到一个按照指定宽度和高度缩放以后的Image实例,然后再用setImage方法设置ImageIcon所显示的图像\x0d\x0a一下示例:\x0d\x0aimportjavax.swing.*;\x0d\x0aimportjava.awt.*;\x0d\x0a\x0d\x0apublicclasstestextendsJFrame\x0d\x0a{\x0d\x0aprivateImageIconimg;\x0d\x0aprivateJLabelshowImg;\x0d\x0a\x0d\x0aprivatefinalstaticintWIDTH=147;\x0d\x0aprivatefinalstaticintHEIGHT=136;\x0d\x0a\x0d\x0apublictest()\x0d\x0a{\x0d\x0aimg=newImageIcon("1.png");\x0d\x0aimg.setImage(img.getImage().getScaledInstance(test.WIDTH,test.HEIGHT,Image.SCALE_DEFAULT));\x0d\x0a\x0d\x0ashowImg=newJLabel();\x0d\x0ashowImg.setIcon(img);\x0d\x0a\x0d\x0athis.add(showImg,BorderLayout.CENTER);\x0d\x0athis.setBounds(300,200,400,300);\x0d\x0athis.pack();\x0d\x0athis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\x0d\x0athis.setVisible(true);\x0d\x0a}\x0d\x0a\x0d\x0apublicstaticvoidmain(Stringargs[])\x0d\x0a{\x0d\x0anewtest();\x0d\x0a}\x0d\x0a}
java 图片缩放代码
直接给你一个类,直接套用就好了
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
public class Resize {
BufferedImage bufImage;
int width;
int height;
public Resize() {
// TODO Auto-generated constructor stub
}
public Resize(String srcPath,int width,int height) {
this.width = width;
this.height = height;
try{
this.bufImage = ImageIO.read(new File(srcPath));
}catch(Exception e){
e.printStackTrace();
}
}
public static BufferedImage rize(BufferedImage srcBufImage,int width,int height){
BufferedImage bufTarget = null;
double sx = (double) width / srcBufImage.getWidth();
double sy = (double) height / srcBufImage.getHeight();
int type = srcBufImage.getType();
if(type == BufferedImage.TYPE_CUSTOM){
ColorModel cm = srcBufImage.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(width,
height);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null);
}else
bufTarget = new BufferedImage(width, height, type);
Graphics2D g = bufTarget.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(srcBufImage, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return bufTarget;
}
}
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代码缩小照片 java对图片进行缩放
文章URL:http://ybzwz.com/article/ddceepp.html