java Graphics graphics = image.createGraphics(); 为什么不能显示。怎么才能让新图片在另一个类输出
publicclassMaxMinPictuextendsJFrame{//publicstaticvoidresizeImage(FileInputStreamis,i...
public class MaxMinPictu extends JFrame{
//
public static void resizeImage(FileInputStream is,int size) throws IOException {
System.out.println("yyyyyyyyyyyyyyy");
//String file1 = "file/e.png";
//File file2 = new File(file);
BufferedImage src = ImageIO.read(is);
double width = src.getWidth();
double height = src.getHeight();
System.out.println("---00000-----"+width+"----000000000---"+height);
// size=40;
double percent = size/width;
int newW = (int)(width*percent);
int newH = (int)(height*percent);
System.out.println("---"+newW+"====="+newH);
BufferedImage image = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_BGR);
Graphics graphics = image.createGraphics();
graphics.drawImage(src, 0, 0, newW, newH, null);
//ImageIO.write(image, "png", arg2);
}
} 展开
//
public static void resizeImage(FileInputStream is,int size) throws IOException {
System.out.println("yyyyyyyyyyyyyyy");
//String file1 = "file/e.png";
//File file2 = new File(file);
BufferedImage src = ImageIO.read(is);
double width = src.getWidth();
double height = src.getHeight();
System.out.println("---00000-----"+width+"----000000000---"+height);
// size=40;
double percent = size/width;
int newW = (int)(width*percent);
int newH = (int)(height*percent);
System.out.println("---"+newW+"====="+newH);
BufferedImage image = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_BGR);
Graphics graphics = image.createGraphics();
graphics.drawImage(src, 0, 0, newW, newH, null);
//ImageIO.write(image, "png", arg2);
}
} 展开
1个回答
展开全部
package test;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class MaxMinPictu extends JFrame {
//
public static void resizeImage(FileInputStream is, int size)
throws IOException {
System.out.println("yyyyyyyyyyyyyyy");
// String file1 = "file/e.png";
// File file2 = new File(file);
BufferedImage src = ImageIO.read(is);
double width = src.getWidth();
double height = src.getHeight();
System.out.println("---00000-----" + width + "----000000000---"
+ height);
// size=40;
double percent = size / width;
int newW = (int) (width * percent);
int newH = (int) (height * percent);
System.out.println("---" + newW + "=====" + newH);
BufferedImage image = new BufferedImage(newW, newH,
BufferedImage.TYPE_INT_BGR);
Graphics graphics = image.createGraphics();
graphics.drawImage(src, 0, 0, newW, newH, null);
ImageIO.write(image, "png", new File("C:/Users/sunjunjie/Desktop/abc.png"));//图片的输出路径
}
public static void main(String[] args) {
try {
new MaxMinPictu().resizeImage(new FileInputStream(new File("C:/Users/sunjunjie/Desktop/down.png")), 500);//图片的源路径
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
朋友,你的代码我稍作修改,并写了调用方法;测试通过;挺辛苦了,记得采纳~~~
追问
谢谢
我想让另一个类接受新图片的信息,
我的目的是把源图像缩小后,,得到新图片信息(宽,高,像素什么的)
是要用到什么方法么?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询