/**
* 保存拍照的图片
*
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class SaveCapImage extends JFrame
{
private MainFrame mainFrame;
private Player player;
public SaveCapImage()
{
}
public SaveCapImage(MainFrame mainFrame, Player player)
{
this.mainFrame = mainFrame;
this.player = player;
capImage();
}
/**
* 截图
*/
private void capImage()
{
FrameGrabbingControl fgc = (FrameGrabbingControl) player
.getControl("javax.media.control.FrameGrabbingControl");
// 把视频流放在缓冲区
Buffer bufferFrame = fgc.grabFrame();
// 访问缓冲区的视频流
BufferToImage bufferToImage = new BufferToImage(
(VideoFormat) bufferFrame.getFormat());
// 根据缓冲区的视频流产生一幅图像
Image image = bufferToImage.createImage(bufferFrame);
int imageHeight = image.getWidth(this);
int imageWidth = image.getHeight(this);
setSize(imageHeight, imageWidth);
setFilePath(image);
}
@SuppressWarnings("deprecation")
private void setFilePath(Image image)
{
FileDialog fd = new FileDialog(mainFrame, "保存截图", FileDialog.SAVE);
fd.show();
String path = fd.getDirectory();
String fileName = path + "\\" + fd.getFile() + ".jpg";
File file = new File(fileName);
if (file.exists())
{
int value = JOptionPane.showConfirmDialog(this, "确定替换吗?","提示", JOptionPane.OK_CANCEL_OPTION);
if(value == JOptionPane.CANCEL_OPTION)
{
setFilePath(image);
return;
}
}
if (path != null)
{
if (saveJPEG(fileName,image))//保存
{
JOptionPane.showMessageDialog(this, "图片保存成功");
//setVisible(false);
dispose();
}
else
{
JOptionPane.showMessageDialog(this,
"保存图片失败 ");
}
}
}
/**
* 保存
*
* @param filename
* @param image
* @return
*/
private boolean saveJPEG(String filename, Image image)
{
boolean saved = false;
BufferedImage bi = new BufferedImage(image.getWidth(null), image
.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(image, null, null);
FileOutputStream out = null;
try
{
out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1.0f, false); // 100% high quality setting, no
// compression
encoder.setJPEGEncodeParam(param);
encoder.encode(bi);
out.close();
saved = true;
}
catch (Exception ex)
{
System.out.println("Error saving JPEG : " + ex.getMessage());
}
return saved;
}
}
* 保存拍照的图片
*
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class SaveCapImage extends JFrame
{
private MainFrame mainFrame;
private Player player;
public SaveCapImage()
{
}
public SaveCapImage(MainFrame mainFrame, Player player)
{
this.mainFrame = mainFrame;
this.player = player;
capImage();
}
/**
* 截图
*/
private void capImage()
{
FrameGrabbingControl fgc = (FrameGrabbingControl) player
.getControl("javax.media.control.FrameGrabbingControl");
// 把视频流放在缓冲区
Buffer bufferFrame = fgc.grabFrame();
// 访问缓冲区的视频流
BufferToImage bufferToImage = new BufferToImage(
(VideoFormat) bufferFrame.getFormat());
// 根据缓冲区的视频流产生一幅图像
Image image = bufferToImage.createImage(bufferFrame);
int imageHeight = image.getWidth(this);
int imageWidth = image.getHeight(this);
setSize(imageHeight, imageWidth);
setFilePath(image);
}
@SuppressWarnings("deprecation")
private void setFilePath(Image image)
{
FileDialog fd = new FileDialog(mainFrame, "保存截图", FileDialog.SAVE);
fd.show();
String path = fd.getDirectory();
String fileName = path + "\\" + fd.getFile() + ".jpg";
File file = new File(fileName);
if (file.exists())
{
int value = JOptionPane.showConfirmDialog(this, "确定替换吗?","提示", JOptionPane.OK_CANCEL_OPTION);
if(value == JOptionPane.CANCEL_OPTION)
{
setFilePath(image);
return;
}
}
if (path != null)
{
if (saveJPEG(fileName,image))//保存
{
JOptionPane.showMessageDialog(this, "图片保存成功");
//setVisible(false);
dispose();
}
else
{
JOptionPane.showMessageDialog(this,
"保存图片失败 ");
}
}
}
/**
* 保存
*
* @param filename
* @param image
* @return
*/
private boolean saveJPEG(String filename, Image image)
{
boolean saved = false;
BufferedImage bi = new BufferedImage(image.getWidth(null), image
.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(image, null, null);
FileOutputStream out = null;
try
{
out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1.0f, false); // 100% high quality setting, no
// compression
encoder.setJPEGEncodeParam(param);
encoder.encode(bi);
out.close();
saved = true;
}
catch (Exception ex)
{
System.out.println("Error saving JPEG : " + ex.getMessage());
}
return saved;
}
}