欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java JMF拍照,javajmf拍照,JMF拍照,并存储为jp

来源: javaer 分享于  点击 20341 次 点评:179

java JMF拍照,javajmf拍照,JMF拍照,并存储为jp


JMF拍照,并存储为jpg文件

MyCapturer.java

package Filesender;import java.io.*;import javax.media.*;import javax.media.Buffer;import javax.media.format.*;import javax.media.util.*;import javax.media.control.*;import java.awt.*;import java.awt.image.*;import com.sun.image.codec.jpeg.*;public class MyCapturer implements Runnable{    public static Player player=null;    private CaptureDeviceInfo di=null;    private MediaLocator ml=null;    private Buffer buf=null;    private Image img=null;    private VideoFormat vf=null;    private BufferToImage btoi=null;    //private ImagePanel imgpanel=null;    String str="vfw:Microsoft WDM Image Capture (Win32):0";    private int rectX;    private int rectY;    private int rectWidth=300;    private int rectHeight=200;    private int imgWidth=320;    private int imgHeight=240;    Thread thread=null;    public MyCapturer()    {        thread=new Thread(this);  //建立专门的线程        thread.start();        di=CaptureDeviceManager.getDevice(str);        ml=di.getLocator();        try        {            player=Manager.createRealizedPlayer(ml);        }        catch(Exception e)        {            e.printStackTrace();        }        player.start();        sleep(4000);        System.out.println("init OK");    }    public static void playerclose()    {        player.close();        player.deallocate();    }    public void capture()    {        FrameGrabbingControl fgc=(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");        buf=fgc.grabFrame();// Convert it to an image        btoi=new BufferToImage((VideoFormat)buf.getFormat());        img=btoi.createImage(buf);// show the image        sleep(2000);        if(img==null)        {            System.out.println("capture Error");            System.exit(1);        }        //imgpanel.setImage(img); // save image        System.out.println("capture OK");    }    public void save()    {        if(img!=null)        {            String path="JMF_resource/test.jpg";            saveJPG(path);        }    }    public void saveJPG(String s)    {        System.out.println("save start");        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);        File f=new File(s);        if(!f.exists())            try            {                f.createNewFile();            }            catch(IOException e)            {                e.printStackTrace();            }        FileOutputStream out=null;        try        {            out=new FileOutputStream(f);        }        catch(java.io.FileNotFoundException io)        {            System.out.println("File Not Found");        }        JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);        JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(bi);        param.setQuality(1f,false);        encoder.setJPEGEncodeParam(param);        try        {            encoder.encode(bi);            sleep(2000);            out.close();            System.out.println("save OK");        }        catch(java.io.IOException io)        {            System.out.println("IOException");        }    }    private void sleep(int t)    {        try        {            Thread.sleep(t);        }        catch(InterruptedException e)        {            e.printStackTrace();        }    }    public static void main(String[] args) throws Exception    {        MyCapturer m=new MyCapturer();        m.capture();        m.save();        System.out.println("OK");        System.exit(0);    }    public void run()    {        // TODO Auto-generated method stub        sleep(500);    }}
相关栏目:

用户点评