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如何修改图片大小
截取点代码片断,你自己看着改
修改图片大小用getScaledInstance方法
BufferedImage bimg = null;
Image img = null;
int width, height;
try {
bimg = ImageIO.read(u);
if(bimg.getWidth() bimg.getHeight())
{
width = 40;
height = bimg.getHeight() * width / bimg.getWidth();
}
else
{
height = 40;
width = bimg.getWidth() * height / bimg.getHeight();
}
img = bimg.getScaledInstance(width, height, Image.SCALE_DEFAULT);
pre_image = bimg.getScaledInstance(width * 10, height * 10, Image.SCALE_DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
java设置插入图片大小
可以用Image中的getScaledInstance方法得到一个按照指定宽度和高度缩放以后的Image实例,然后再用setImage方法设置ImageIcon所显示的图像
一下示例:
×××××××××××××××××××××××××××××
import javax.swing.*;
import java.awt.*;
public class test extends JFrame
{
private ImageIcon img;
private JLabel showImg;
private final static int WIDTH=147;
private final static int HEIGHT=136;
public test()
{
img=new ImageIcon("1.png");
img.setImage(img.getImage().getScaledInstance(test.WIDTH,test.HEIGHT,Image.SCALE_DEFAULT));
showImg=new JLabel();
showImg.setIcon(img);
this.add(showImg,BorderLayout.CENTER);
this.setBounds(300,200,400,300);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String args[])
{
new test();
}
}
java 通过 路径 来调用已有图片(或其他任何文件)的代码怎么写?
Class.getResource("") 返回的是当前Class这个类所在包开始的位置
举例
URL url = XXXX.class.getResource("");
System.out.println(url);
//输出file:/D:/space/workspace/JDK8/bin/a/
所以a文件下的图片 不需要写 /1.jpg 而直接写 1.jpg
URL url = XXXX.class.getResource("1.jpg");
ImageIcon对象的构造方法很多
比如写图片路径来构造
相对路径的写法
ImageIcon icon = new ImageIcon("src\\a\\50.png");
完整路径,
ImageIcon icon = new ImageIcon("D:\\space\\workspace\\JDK8\\src\\a\\50.png");
使用完整路径,可以加载电脑上的其他位置的图片
比如 ImageIcon icon = new ImageIcon("c:\\50.png");
在图片上右键单击------选择Properties-------弹出对话框里有Location信息
这个信息就是完整路径
高分求java代码,用来生成图片文件!!
//defaultSuffix是jpg
public static final boolean resizeImage(String fileName, String suffix) throws Exception {
boolean uploaded = false;
BufferedImage input;
if(suffix.equalsIgnoreCase("tif")||suffix.equalsIgnoreCase("tiff")||suffix.equalsIgnoreCase("png")) {
RenderedImage image = JAI.create("fileload", TurbineServlet.getRealPath(imageRoot + fileName+"."+suffix));
WritableRaster raster = image.copyData(null);
BufferedImage bi = new BufferedImage( image.getColorModel(), raster, true, null);
BufferedImage bi2 = new BufferedImage( maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi2.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.setBackground(java.awt.Color.WHITE);//把tiff和png转换后的背景设置为白色
g2.fillRect(0, 0, maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth());
g2.drawImage(bi, 0, 0, maxResizeDimension, bi.getHeight()*maxResizeDimension/bi.getWidth(), null);
PlanarImage pi = PlanarImage.wrapRenderedImage(bi2);
JAI.create("FileStore", pi, TurbineServlet.getRealPath(resizeRoot + fileName+"."+defaultSuffix).replaceAll("\\\\","\\\\\\\\"), "JPEG", new JPEGEncodeParam());
input = pi.getAsBufferedImage();
int w = maxThumbDimension, h = maxThumbDimension;
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = output.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(input,0,0,w,h, null);
ImageIO.write(output, defaultSuffix, new File(TurbineServlet.getRealPath(thumbRoot + fileName+"."+defaultSuffix)));
}
else {
input = ImageIO.read(new File(TurbineServlet.getRealPath(imageRoot + fileName+"."+suffix)));
int w = maxThumbDimension, h = maxThumbDimension;
BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = output.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(input,0,0,w,h, null);
ImageIO.write(output, defaultSuffix, new File(TurbineServlet.getRealPath(thumbRoot + fileName+"."+defaultSuffix)));
BufferedImage output2 = new BufferedImage(maxResizeDimension, input.getHeight()*maxResizeDimension/input.getWidth(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2 = output2.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(input,0,0,maxResizeDimension, input.getHeight()*maxResizeDimension/input.getWidth(), null);
ImageIO.write(output2, defaultSuffix, new File(TurbineServlet.getRealPath(resizeRoot + fileName+"."+defaultSuffix)));
}
uploaded = true;
return uploaded;
}
网站栏目:java调准图片代码,java调整图片大小
网站网址:http://scyanting.com/article/dsgdjdg.html