===============================================
这是有关JMF的一个例子。我试过了好用。
package Media;import javax.swing.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*; import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*; import com.sun.image.codec.jpeg.*; public class testjmf extends Applet implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton capture = null; //拍照按钮
private JButton save = null; //保存按钮
private JTextField num = null; //输入图片文件名
private ImagePanel imgpanel = null; //取像面板
//摄像头控制
public static Player player = null;
private CaptureDeviceInfo di = null;
private MediaLocator ml = null;
//从摄像头获取照图片
private Buffer buf = null;
private BufferToImage btoi = null;
private Image img = null;
private int rectX;
private int rectY;
private int rectWidth = 150; //选取区域宽度
private int rectHeight = 200; //选取区域高度
private int imgWidth = 320; //图像面板宽度
private int imgHeight = 240; //图像面板高度
private String fname = "test"; //图片文件名称 public testjmf()
{ setLayout(new BorderLayout());
setSize(640, 300);
imgpanel = new ImagePanel();
imgpanel.addMouseMotionListener(imgpanel);
capture = new JButton("拍摄");
capture.addActionListener(this);
save = new JButton("保存所选区域图片");
save.addActionListener(this);
num = new JTextField();
num.setColumns(10);
//摄像头驱动,需要先用jmf register detect以后来确定是哪个驱动
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = di.getLocator();
try
{ player = Manager.createRealizedPlayer(ml);
player.start();
Component comp;
Panel panel1 = new Panel();
Panel panel2 = new Panel();
if ((comp = player.getVisualComponent()) != null) {
panel1.add(comp);
}
panel1.add(imgpanel);
panel2.add(new Label("请输入文件名:"));
panel2.add(num);
panel2.add(capture);
panel2.add(save);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
}
catch (Exception e) { e.printStackTrace(); }
}
public static void main(String[] args) {
Frame f = new Frame("现场拍照程序测试版");
testjmf cf = new testjmf();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { playerclose(); //退出时关闭player
System.exit(0); }
});
f.add("Center", cf);
f.setSize(640,300);
f.pack();
f.setVisible(true);
}
public static void playerclose() {
player.close();
player.deallocate();
}
public void actionPerformed(ActionEvent e) {
JComponent c = (JComponent) e.getSource();
if (c == capture) { //抓图生成图片后放入imgpanel面板中
FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
btoi = new BufferToImage((VideoFormat) buf.getFormat());
img = btoi.createImage(buf);
imgpanel.setImage(img);
}
else if (c == save) { //保存选定区域的图片至文件
if (img != null) {
fname = !num.getText().equals("") ? num.getText() : "test";
saveJPG(img, "D:/test1/web/photo/" + fname + ".jpg");
}
}
}
class ImagePanel extends Panel implements MouseMotionListener {
private Image myimg = null;
public ImagePanel() {
setLayout(null);
setSize(imgWidth, imgHeight);
}
public void setImage(Image img) {
this.myimg = img;
repaint();
}
public void update(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
if (myimg != null) {
g.drawImage(myimg, 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 e) { }
}
public void saveJPG(Image img, String s) {
BufferedImage bi = (BufferedImage) createImage(imgWidth, imgHeight);
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;
JPEGImageEncoder encoder = null;
System.out.println("文件名:-->"+s);
try { out = new FileOutputStream(new File(s));
encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1f, false);
encoder.setJPEGEncodeParam(param);
}catch(Exception e) {e.printStackTrace();}
finally{ try { encoder.encode(bi);
if (out!=null) out.close(); }
catch (java.io.IOException io) { System.out.println("IOException"); }
}
}
}
===================================================
这是有关JMF的一个例子。我试过了好用。
package Media;import javax.swing.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*; import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*; import com.sun.image.codec.jpeg.*; public class testjmf extends Applet implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton capture = null; //拍照按钮
private JButton save = null; //保存按钮
private JTextField num = null; //输入图片文件名
private ImagePanel imgpanel = null; //取像面板
//摄像头控制
public static Player player = null;
private CaptureDeviceInfo di = null;
private MediaLocator ml = null;
//从摄像头获取照图片
private Buffer buf = null;
private BufferToImage btoi = null;
private Image img = null;
private int rectX;
private int rectY;
private int rectWidth = 150; //选取区域宽度
private int rectHeight = 200; //选取区域高度
private int imgWidth = 320; //图像面板宽度
private int imgHeight = 240; //图像面板高度
private String fname = "test"; //图片文件名称 public testjmf()
{ setLayout(new BorderLayout());
setSize(640, 300);
imgpanel = new ImagePanel();
imgpanel.addMouseMotionListener(imgpanel);
capture = new JButton("拍摄");
capture.addActionListener(this);
save = new JButton("保存所选区域图片");
save.addActionListener(this);
num = new JTextField();
num.setColumns(10);
//摄像头驱动,需要先用jmf register detect以后来确定是哪个驱动
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = di.getLocator();
try
{ player = Manager.createRealizedPlayer(ml);
player.start();
Component comp;
Panel panel1 = new Panel();
Panel panel2 = new Panel();
if ((comp = player.getVisualComponent()) != null) {
panel1.add(comp);
}
panel1.add(imgpanel);
panel2.add(new Label("请输入文件名:"));
panel2.add(num);
panel2.add(capture);
panel2.add(save);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
}
catch (Exception e) { e.printStackTrace(); }
}
public static void main(String[] args) {
Frame f = new Frame("现场拍照程序测试版");
testjmf cf = new testjmf();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { playerclose(); //退出时关闭player
System.exit(0); }
});
f.add("Center", cf);
f.setSize(640,300);
f.pack();
f.setVisible(true);
}
public static void playerclose() {
player.close();
player.deallocate();
}
public void actionPerformed(ActionEvent e) {
JComponent c = (JComponent) e.getSource();
if (c == capture) { //抓图生成图片后放入imgpanel面板中
FrameGrabbingControl fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
btoi = new BufferToImage((VideoFormat) buf.getFormat());
img = btoi.createImage(buf);
imgpanel.setImage(img);
}
else if (c == save) { //保存选定区域的图片至文件
if (img != null) {
fname = !num.getText().equals("") ? num.getText() : "test";
saveJPG(img, "D:/test1/web/photo/" + fname + ".jpg");
}
}
}
class ImagePanel extends Panel implements MouseMotionListener {
private Image myimg = null;
public ImagePanel() {
setLayout(null);
setSize(imgWidth, imgHeight);
}
public void setImage(Image img) {
this.myimg = img;
repaint();
}
public void update(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
if (myimg != null) {
g.drawImage(myimg, 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 e) { }
}
public void saveJPG(Image img, String s) {
BufferedImage bi = (BufferedImage) createImage(imgWidth, imgHeight);
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;
JPEGImageEncoder encoder = null;
System.out.println("文件名:-->"+s);
try { out = new FileOutputStream(new File(s));
encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1f, false);
encoder.setJPEGEncodeParam(param);
}catch(Exception e) {e.printStackTrace();}
finally{ try { encoder.encode(bi);
if (out!=null) out.close(); }
catch (java.io.IOException io) { System.out.println("IOException"); }
}
}
}
===================================================
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货