⑴ 求一个用BufferedImage这个改变图片大小的java代码!~!
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageZipUtil {
/**
* 压缩图片文件<br>
* 先保存原文件,再压缩、上传
*
* @ oldFile
* 要进行压缩的文件全路径
* @param width
* 宽度
* @param height
* 高度
* @param quality
* 质量
* @param smallIcon
* 小图片的后缀
* @return 返回压缩后的文件的全路径
*/
public String zipImageFile(String oldFile, int width, int height,
float quality, String smallIcon) {
if (oldFile == null) {
return null;
}
String newImage = null;
try {
/** 对服务器上的临时文件进行处理 */
Image srcFile = ImageIO.read(new File(oldFile));
int w = srcFile.getWidth(null);
System.out.println(w);
System.out.println(smallIcon);
System.out.println(smallIcon);
int h = srcFile.getHeight(null);
System.out.println(h);
//width = w/4;
//height = h/4;
/** 宽,高设定 */
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
String filePrex = oldFile.substring(0, oldFile.indexOf('.'));
/** 压缩后的文件名 */
newImage = filePrex + smallIcon
+ oldFile.substring(filePrex.length());
/** 压缩之后临时存放位置 */
FileOutputStream out = new FileOutputStream(newImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/** 压缩质量 */
jep.setQuality(quality, true);
encoder.encode(tag, jep);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newImage;
}
/**
* 保存文件到服务器临时路径
*
* @param fileName
* @param is
* @return 文件全路径
*/
public String writeFile(String fileName, InputStream is) {
if (fileName == null || fileName.trim().length() == 0) {
return null;
}
try {
/** 首先保存到临时文件 */
FileOutputStream fos = new FileOutputStream(fileName);
byte[] readBytes = new byte[512];// 缓冲大小
int readed = 0;
while ((readed = is.read(readBytes)) > 0) {
fos.write(readBytes, 0, readed);
}
fos.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
public static void main(String[] args){
ImageZipUtil u = new ImageZipUtil();
u.zipImageFile("e:/SAM_0006.JPG", 128, 128, 1f, "x2");
}
}
⑵ 在java中如何利用事件实现图片大小的改变
应该是满足了吧
import java.awt.*;
import java.applet.*;
import java.net.URL;
import java.awt.image.ImageProcer;
public class AppletTest extends Applet {
private Image i;
String urlString = "http://www..com/img/logo.gif"; // 图片地址
Button b1 = new Button("zoom In");
Button b2 = new Button("zoom Out");
int a = 80;
int b = 80;
public void init() {
try {
URL url = new URL(urlString);
i = this.createImage((ImageProcer) url.getContent());
add(b1);
add(b2);
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean action(Event evt, Object arg) {
if(evt.target.equals(b1))
zoomIn();
else if(evt.target.equals(b2))
zoomOut();
// Let the base class handle it:
else
return super.action(evt, arg);
return true; // We've handled it here
}
public void zoomIn(){
a += 10;
b += 10;
repaint();
}
public void zoomOut(){
a -= 10;
b -= 10;
repaint();
}
public void paint(Graphics g) {
g.drawImage(i, 20, 40, a, b, this);
}
}
⑶ java 用这个方法如何设置图片大小
看你想按图片的实际大小、还是按当前组件(ballgame)的大小。
按实际大小
g.drawImage(sun, sun.getWidth(), sun.getHeight(),null);
组件大小
g.drawImage(sun, getWidth(), getHeight(),null);
⑷ 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 swing中如何改变图片大小,如下图
你首先要确认一下面板的布局方式。
你没有加图片时,已经将label的图标设置为了图片,也没有设置标签的大小,所以在添加标签到面板时,就会站整个面板,所以可以看到图片。
当你加上时,icon直接加到面板,这种方式我没尝试过,可能会影响左上角的图标,也可能像你这种情况看不到。建议使用java.awt.image;而不用用imageicon
⑹ JAVA 图片大小设置
应该是路径问题。
把你要显示的图片放在你的Test类里,
再把 :con = new ImageIcon("D:\\java程序回\\1.jpg");
改成 : icon = new ImageIcon("./1.jpg");
你试试,看行不答。
⑺ java怎么设置插入图片大小
用抄Image中的getScaledInstance方法得到一个按照指定宽度和高度缩放以后的Image实例,然后再用setImage方法设置ImageIcon所显示的图像。
⑻ java程序中如何设置图片大小
用Image中的tance方法得到一个按照指定宽度和高度缩放以后的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改变图片大小,该怎么处理
/**
* 图片缩放
*
* @param image 源图像对象。
* @param xscale 图像 x 轴(宽度)上的的缩放比例。
* @param yscale 图像 y 轴(高度)上的的缩放比例。
* @return 缩放后的图像对象。
*/
public static BufferedImage zoomImage(BufferedImage image, double xscale, double yscale) {
int width = (int) ((double) image.getWidth() * xscale);
int height = (int) ((double) image.getHeight() * yscale);
AreaAveragingScaleFilter areaAveragingScaleFilter = new AreaAveragingScaleFilter(width, height);
FilteredImageSource filteredImageSource = new FilteredImageSource(image.getSource(), areaAveragingScaleFilter);
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = result.getGraphics();
Canvas canvas = new Canvas();
g.drawImage(canvas.createImage(filteredImageSource), 0, 0, null);
return result;
}