新手求教,怎么打开电脑里的图片和保存图像…需要用到什么知识
代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.*;
import java.io.File;
public class Pic extends JFrame implements ActionListener
{
private JButton
b1 = new JButton("打开"),
b2 = new JButton("button2"),
b3 = new JButton("button3"),
b4 = new JButton("button4"),
b6 = new JButton("保存"),
b5 = new JButton("button5");
private Panel panel1 = new Panel();
private JPanel panel2 = new JPanel();
public Pic()
{
this.setTitle("图像处理");
this.setSize(900, 700);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.img = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/Desert.jpg"));
panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(b4);
panel2.add(b5);
panel2.add(b6);
panel1.repaint();
panel1.setBackground(Color.darkGray);
panel2.setBackground(Color.CYAN);
this.add(panel1,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);
}
public static void main(String[] args)
{
Pic pic = new Pic();
}
public void actionPerformed(ActionEvent arg0) {
}
}
class Panel extends JPanel
{
Image img = null;
int height,width;
public void paint(Graphics g)
{
height = img.getHeight(null);
width = img.getWidth(null);
if(height > 600&&width > 900)
{
height = height/2;
width = width/2;
}
super.paint(g);
g.drawImage(img,(this.getWidth()-width)/2,(this.getHeight()-height)/2,width,height,this);
System.out.print("width="+width+" height="+height);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.*;
import java.io.File;
public class Pic extends JFrame implements ActionListener
{
private JButton
b1 = new JButton("打开"),
b2 = new JButton("button2"),
b3 = new JButton("button3"),
b4 = new JButton("button4"),
b6 = new JButton("保存"),
b5 = new JButton("button5");
private Panel panel1 = new Panel();
private JPanel panel2 = new JPanel();
public Pic()
{
this.setTitle("图像处理");
this.setSize(900, 700);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.img = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/Desert.jpg"));
panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(b4);
panel2.add(b5);
panel2.add(b6);
panel1.repaint();
panel1.setBackground(Color.darkGray);
panel2.setBackground(Color.CYAN);
this.add(panel1,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);
}
public static void main(String[] args)
{
Pic pic = new Pic();
}
public void actionPerformed(ActionEvent arg0) {
}
}
class Panel extends JPanel
{
Image img = null;
int height,width;
public void paint(Graphics g)
{
height = img.getHeight(null);
width = img.getWidth(null);
if(height > 600&&width > 900)
{
height = height/2;
width = width/2;
}
super.paint(g);
g.drawImage(img,(this.getWidth()-width)/2,(this.getHeight()-height)/2,width,height,this);
System.out.print("width="+width+" height="+height);
}
}
...全文
相关推荐
利用C++打开和保存图像文件
`以上代码片段展示了在C++环境中利用MFC与GDI+或OpenCV进行图像文件的打开和保存操作。
打开和保存图片函数.rar_图片打开_打开和保存图像函数C#
通过理解这些基本概念和操作,你可以轻松地在C#应用程序中实现OpenCV的图片打开和保存功能,进一步扩展到图像处理和分析的更复杂任务。
MFC 图像的打开和保存实现
MFC提供了一些类,如CImage、CDC和CBitmap,来支持图像处理。本篇文章将深入探讨如何在MFC中实现图像的打开和保存功能,并提供详细的知识点讲解。
用python处理图片之打开\显示\保存图像的方法
这篇文章将深入介绍如何使用Python打开、显示和保存图像,主要依赖于Pillow库,它是Python Imaging Library (PIL) 的分支并持续维护。
OpenCV获取视频的每一帧并保存为.jpg图片
以上便是本知识点的主要内容,通过此知识点的介绍,可以了解到如何使用OpenCV来操作视频文件,获取视频的每一帧,并将每一帧保存为.jpg格式的图片。


Java SE
