For the first time blogging, the boss asked to use JAVACV to achieve the camera function
All kinds of Baidu add experiment, record also don't know what bug, very unprofessional ...
First prepare jar package, Source: http://blog.csdn.net/eguid_1/article/details/51659578
Import the Jia package according to the instructions in the above article
javacv.jar,javacpp.jar,ffmpeg.jar,ffmpeg-System Platform-jar,opencv.jar,opencv-system platform-jar
All my headaches are layout problems, size problems. This part of SE hasn't been studied.
Main problem encountered: Iplimage does not have a way to get the BufferedImage object, you need to convert
public class Demo {public static void main (string[] args) throws Exception, IOException, interruptedexception {Open
Cvframegrabber grabber = new Opencvframegrabber (0);
Grabber.setimagewidth (800);
Grabber.setimageheight (640);
Grabber.start ();
Opencvframeconverter.toiplimage converter = new Opencvframeconverter.toiplimage ();
Iplimage grabbedimage = Converter.converttoiplimage (Grabber.grab ());
Canvasframe frame = new Canvasframe ("Some Title", Canvasframe.getdefaultgamma ()/Grabber.getgamma ());
Frame.setsize (800, 600);
Frame.setbounds (200, 100, 640, 640);
Set the operation interface JPanel ContentPane = new JPanel ();
Contentpane.setbounds (0, 0, 640, 640);
Container contentPane2 = Frame.getcontentpane ();
JButton Take_photo = new JButton ("photographed");
JButton Save_photo = new JButton ("Save");
JButton cancle = new JButton ("close");
Camera Camera = new Camera ();
Monitor shooting Take_photo.addmouselistener (new Takephotomouseadapter (Take_photo, camera)); Monitor Save Save_photo.adDmouselistener (New Savephotomouseadapter (grabbedimage)); Close Cancle.addmouselistener (new Mouseadapter () {public void mouseclicked (MouseEvent arg0) {frame.setvisible (f
Alse);
}
});
Add button Contentpane.add (Take_photo, Borderlayout.south);
Contentpane.add (Save_photo, Borderlayout.south);
Contentpane.add (cancle, Borderlayout.south);
Add panel Contentpane2.add (ContentPane, Borderlayout.south); Operation state while (Frame.isvisible ()) {//Get image if (Camera.getstate ()) {grabbedimage = Converter.convert (Grabbe
R.grab ());
} frame.showimage (Converter.convert (grabbedimage));
Every 40 milliseconds to refresh the video, 25 frames a second thread.sleep (40);
} frame.dispose ();
Grabber.stop ();
}
}
//click Save
public class Savephotomouseadapter extends Mouseadapter {private iplimage iplimage;
Public Savephotomouseadapter (Iplimage iplimage) {this.iplimage = Iplimage;
@Override public void mouseclicked (MouseEvent arg0) {System.out.println ("save");
Save result Prompt Box JFrame myframe = new JFrame ();
try {if (iplimage!= null) {//Save picture Cvsaveimage ("D://yepao//savedimage.jpg", iplimage); Send modify user avatar request ...
can also send a byte array directly to the server, upload images by the server and modify the user avatar Joptionpane.showmessagedialog (myframe, "upload success");
The catch (IOException e) {joptionpane.showmessagedialog (MyFrame, "Save Failed");
E.printstacktrace ();
Finally {//close hint JFrame myframe.dispose ();
MyFrame = null;
The public static void Cvsaveimage (String path, iplimage image) throws IOException {File File = new file (path);
Imageio.write (Tobufferedimage (image), "JPG", file);
Use bytes to save bytearrayoutputstream out = new Bytearrayoutputstream ();
Imageio.write (Tobufferedimage (image), "JPG", out); Byte[] bs = Out.tobytearray ();
Save byte array for picture to local fileoutputstream fos = new FileOutputStream (file);
Fos.write (BS, 0, Bs.length);
Fos.close ();
Out.close (); ///Get bufferedimage by image
Iplconverter can view the objects that the Convert method can convert, and the Frame iplimage,mat transitions between
Java2dframeconverter let frame and bufferedimage convert to each other public
static BufferedImage tobufferedimage (iplimage image) {
toiplimage iplconverter = new Opencvframeconverter.toiplimage ();
Java2dframeconverter java2dconverter = new Java2dframeconverter ();
BufferedImage bufferedimage = Java2dconverter.convert (Iplconverter.convert (image));
Return BufferedImage
}
}
Toggle Image Display
public class Takephotomouseadapter extends Mouseadapter {
private JButton JButton;
Private Camera Camera;
Public Takephotomouseadapter (JButton JButton, Camera Camera) {
This.jbutton = JButton;
This.camera = camera;
}
@Override public
void mouseclicked (MouseEvent arg0) {
System.out.println ("photographed");
Modify Display
if (camera.getstate ()) {
Jbutton.settext ("continue to photograph");
Pause
the photo camera.setstate (false);
} else {
jbutton.settext ("photo");
Continue taking pictures
camera.setstate (true);}}
Do you want to draw a picture
public class Camera {
private Boolean state = true;
Public Boolean GetState () {return state
;
}
public void SetState (Boolean state) {
this.state = state;
}
}
Finally found that I lift the left hand, the video inside I have something. And then you need to flip the flip method from org.bytedeco.javacpp.opencv_core.* to the image level.
Mat Mat = new Mat ();
int flipcode = 1;
Flip (Converter.converttomat (frame to flip), Mat, Flipcode);
You still need to convert mat when you save
BufferedImage bufferedimage = Java2dconverter.convert (Convert.convert (MAT));