📄 photoframe.java
字号:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/*
* 创建日期 2006-11-20
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class PhotoFrame extends JFrame implements MouseMotionListener{
private JPanel photoPanel;
/**
* 选取框的x,y,width,height参数的默认值
*/
private int rectX;
private int rectY;
private static int rectWidth = 100; //照片宽
private static int rectHeight = 120; //照片高
private static String photoURL = "Photo"; //照片保存路径
private int imgWidth = 320;
private int imgHeight = 240;
private Image photo = null;
public PhotoFrame(){
super("照片预览");
this.setSize(imgWidth,imgHeight);
//photoPanel
photoPanel = new JPanel();
photoPanel.setSize(imgWidth, imgHeight);
this.addMouseMotionListener(this);
this.add(photoPanel,BorderLayout.NORTH);
this.setVisible(true);
this.setResizable(false);
}
public void setPhoto(Image img) {
// TODO 自动生成方法存根
photo = img;
}
public static void initPhoto(int width,int height,String url){
rectWidth = width;
rectHeight = height;
photoURL = url;
}
public void update(Graphics g)
{
g.clearRect(0, 0, getWidth(), getHeight());
if (photo != null)
{
g.drawImage(photo, 0, 0, this);
g.setColor(Color.RED);
g.drawRect(rectX, rectY, rectWidth, rectHeight);
}
}
public void paint(Graphics g)
{
update(g);
}
public void mouseDragged(MouseEvent e)
{
rectX = e.getX() - 50;
rectY = e.getY() - 50;
repaint();
}
public void mouseMoved(MouseEvent arg0) {
// TODO 自动生成方法存根
}
/**
* 保存图像
* @param img
*/
public void savePhoto(Image img,String name)
{
BufferedImage bi = (BufferedImage) createImage(imgWidth, imgHeight);
/*BufferedImage bi =
new BufferedImage(
img.getWidth(null),
img.getHeight(null),
BufferedImage.TYPE_INT_RGB);*/
Graphics2D g2 = bi.createGraphics();
g2.clipRect(rectX, rectY, rectWidth, rectHeight);
g2.drawImage(img, null, null);
int moveX = rectX > 0 ? rectX : 0;
int moveY = rectY > 0 ? rectY : 0;
int cutWidth =
rectX + rectWidth > imgWidth
? rectWidth - ((rectX + rectWidth) - imgWidth)
: rectWidth;
int cutHeight =
rectY + rectHeight > imgHeight
? rectHeight - ((rectY + rectHeight) - imgHeight)
: rectHeight;
bi = bi.getSubimage(moveX, moveY, cutWidth, cutHeight);
FileOutputStream out = null;
try
{
out = new FileOutputStream(photoURL+"/"+name+".jpg");
}
catch (java.io.FileNotFoundException io)
{
JOptionPane.showMessageDialog(null, " 找不到指定文件加!", "提示",
JOptionPane.INFORMATION_MESSAGE);
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1f, false);
encoder.setJPEGEncodeParam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (java.io.IOException io)
{
System.out.println("IOException");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -