Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中图像的显示与写入_Java_Image - Fatal编程技术网

java中图像的显示与写入

java中图像的显示与写入,java,image,Java,Image,我无法显示图像,因为主方法存在问题,我不确定是否正确写入图像。对不起,我对Java真的很陌生 public class Display extends JPanel { String path = "C:/Users/asus/workspace/Code/src/7horses.jpg"; File file = new File(path); static BufferedImage img; img = ImageIO.read(new File(file)); public sta

我无法显示图像,因为主方法存在问题,我不确定是否正确写入图像。对不起,我对Java真的很陌生

public class Display extends JPanel
{
String path = "C:/Users/asus/workspace/Code/src/7horses.jpg";
File file = new File(path); 

static BufferedImage img;
img = ImageIO.read(new File(file));

public static BufferedImage CopyImage (File file)
{
    BufferedImage cImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = cImg.createGraphics();
    g.drawImage(img, 0, 0, null);
    g.dispose();  
    File saveImage = new File("C:/Users/asus/workspace/Code/src", saveAs);
    ImageIO.write(cImg, "png", saveImage); 
    return cImg;
}

public static void main(String[] args) 
{
    JFrame f = new JFrame("Show Image");
    LoadImage panel = new BufferedImage CopyImage(file);//
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(panel);
    f.pack();
    f.setLocation(200,200);
    f.setVisible(true);
}

}
复制图像后,我不确定如何显示复制副本。下面是我最初用于加载和显示的代码

public class LoadImage extends JPanel
{
BufferedImage img;
String path = "C:/Users/asus/workspace/MP/src/7horses.jpg";
File file = new File(path);

public void paint(Graphics g) 
{
    g.drawImage(img, 0, 0, null);
}

public LoadImage()
{
    try 
    {
        img = ImageIO.read(file); 
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    }
}

public static void main(String[] args) 
{
    JFrame f = new JFrame("Show Image");
    LoadImage panel = new LoadImage();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(panel);
    f.pack();
    f.setLocation(200,200);
    f.setVisible(true);
}

}

此代码有一些地方出错

  • path
    file
    不能被
    main()
    访问,因为它们不是
    static
    而main是。这意味着当运行
    main()
    时,
    path
    file
    都不存在

  • LoadImage panel=new BuffereImage CopyImage(文件)
    不是有效的Java-我不确定您想做什么,但我想您想删除
    new BuffereImage
    部分

  • img=ImageIO.read(新文件(File))-不能在方法或静态块之外运行此语句,因为它需要处理
    IOException

  • ImageIO.read(新文件(File))-
    文件
    已经是
    文件
    类型,您正在将其传递到类型
    文件
    ,这不是必需的<代码>ImageIO.read(文件)可以吗

  • saveAs
    尚未在任何地方定义

  • ImageIO.write(cImg,“png”,saveImage)
    可以抛出一个
    IOException
    ,因此您需要在它周围添加一个
    try catch
    ,就像在
    LoadImage()
    构造函数中一样,或者将
    throws IOException
    附加到方法签名中,就像这样
    public static BufferedImage CopyImage(文件文件文件)throws IOException

  • 您正在引用
    LoadImage
    ,它似乎正试图以稍微不同的方式执行
    Display
    的操作。我不确定这里该建议什么,因为我不明白其用意,但我想也许可以将
    显示中的
    LoadImage
    替换为
    Display


  • 什么是LoadImage?除非它同时是一个BuffereImage和一个容器,否则您的代码甚至无法编译。
    由于一个类不能同时是一个容器和一个BuffereImage(假设两者都是类,而不是接口,并且Java中不存在类级别的多重继承),因此您的代码无法工作。

    您必须弄清楚如何正确初始化LoadImage实例,只有该类的文档(可能还有源代码)才能帮助您。

    可能:

  • 使用
    ImageIO
    从文件中检索
    buffereImage
  • 使用
    ImagePanel
    ,该面板可以使用缓冲图像在屏幕上显示 您的
    JFrame
  • 更像是这样:

    public class Display extends JPanel {
        private static final long serialVersionUID = 1L;
        private static final String path = "C:/Users/asus/workspace/Code/src/7horses.jpg";
        private static final File file = new File(path);
    
        public static void main(String[] args) throws IOException {
            JFrame f = new JFrame("Show Image");
            BufferedImage image = ImageIO.read(file);
            ImagePanel panel = new ImagePanel(image);//
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setContentPane(panel);
            f.pack();
            f.setLocation(200, 200);
            f.setVisible(true);
        }
    
        public static class ImagePanel extends JPanel {
            private static final long serialVersionUID = 1L;
            private BufferedImage image;
    
            public ImagePanel(BufferedImage image) {
                this.image = image;
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters
            }
        }
    
    }
    

    +0 ; 您不能
    img=ImageIO.read(新文件(File))
    ?嗯,实际上,你可以
    staticbufferedimage img=ImageIO.read(新文件(File))
    将完全按照它应该的方式工作,在这种情况下,您如何处理抛出的
    IOException
    ?要么提供一个包装器方法来抑制/处理异常,要么为此使用
    静态{}
    块-无论如何,我没有指出您可以在初始值设定项中检查异常(因为out显然不能)-我指出,您可以在这样的方法块之外运行语句。将该语句重新表述为“你不能让一个方法调用在方法块之外抛出一个已检查的异常”将是OKI,我想这正是OP的想法——至少我理解他的意思。如果你也像罗斯·德鲁那样指出OPs代码中的错误,你就应该接受这一点。