Java uses OpenCV3.2 for video reading and playback

Source: Internet
Author: User

Java uses OpenCV3.2 for video reading and playback

OpenCV from the 3.x version of its Java Language SDK support video file read and write, which greatly facilitates the vast number of Java language developers to learn and use OPENCV, through the camera or video files to read the content and playback of the frame, complete video content analysis and object tracking and other application development tasks. It can be said that the OpenCV C + + SDK can do most things, in the opencv3.x version of Java can be done, so that many Java developers learn OpenCV open the door.

Implementation ideas

First read the video stream or video file each frame with the OpenCV correlation API, and then through the swing jcomponent component realizes the video each frame update display, I imitated the C + + Highgui inside creates the window and the display image interface, A video Playback window class is implemented based on swing, and each frame read is passed to it for continuous display and playback. Each frame is 100 milliseconds apart, and I am implementing it through the Java thread sleep method.

Run effect-USB camera reads per frame

Run effect-video file reads per frame

Code implementation

Video file Read

 PackageCom.gloomyfish.video.demo;ImportJava.awt.Dimension;ImportJava.awt.image.BufferedImage;ImportOrg.opencv.core.Core;ImportOrg.opencv.core.Mat;ImportOrg.opencv.videoio.VideoCapture; Public  class videodemo {     Public Static void Main(string[] args) {system.loadlibrary (core.native_library_name);//Open camera or video fileVideocapture capture =NewVideocapture ();//capture.open (0);Capture.open ("D:/vcprojects/images/768x576.avi");if(!capture.isopened ()) {System.out.println ("Could not load video data ...");return; }intFrame_width = (int) Capture.get (3);intFrame_height = (int) Capture.get (4); Imagegui GUI =NewImagegui (); Gui.createwin ("OpenCV + Java video read and play demo",NewDimension (Frame_width, frame_height)); Mat frame =NewMat (); while(true) {Booleanhave = Capture.read (frame); Core.flip (frame, frame,1);//Win on camera            if(!have) Break;if(!frame.empty ())                {Gui.imshow (Conver2image (frame));            Gui.repaint (); }Try{Thread.Sleep ( -); }Catch(Interruptedexception e)            {E.printstacktrace (); }        }    } Public StaticBufferedImageConver2image(Mat Mat) {intwidth = Mat.cols ();intHeight = mat.rows ();intdims = Mat.channels ();int[] pixels =New int[Width*height];byte[] Rgbdata =New byte[Width*height*dims]; Mat.get (0,0, Rgbdata); BufferedImage image =NewBufferedImage (width, height, bufferedimage.type_int_argb);intindex =0;intR=0, g=0, b=0; for(introw=0; row for(intCol=0; col<width; col++) {if(dims = =3) {index = row*width*dims + col*dims; b = rgbdata[index]&0xFF; g = rgbdata[index+1]&0xFF; r = rgbdata[index+2]&0xFF; Pixels[row*width+col] = ((255&0xFF) << -) | ((r&0xFF) << -) | ((g&0xFF) <<8) | b&0xFF; }if(dims = =1{index = row*width + col; b = rgbdata[index]&0xFF; Pixels[row*width+col] = ((255&0xFF) << -) | ((b&0xFF) << -) | ((b&0xFF) <<8) | b&0xFF; }}} Setrgb (image,0,0, width, height, pixels);returnImage }/** * A convenience method for setting ARGB pixels on an image.     This tries to avoid the performance * penalty of Bufferedimage.setrgb unmanaging the image. */     Public Static void Setrgb(BufferedImage image,intXintYintWidthintHeightint[] pixels) {intType = Image.gettype ();if(type = = Bufferedimage.type_int_argb | | type = = BUFFEREDIMAGE.TYPE_INT_RGB) image.getraster (). setdataelements (x, y, width, height, pixels);ElseImage.setrgb (x, y, width, height, pixels,0, width); }}

Video and Image display window classes

 PackageCom.gloomyfish.video.demo;ImportJava.awt.BorderLayout;ImportJava.awt.Color;ImportJava.awt.Dimension;ImportJava.awt.Graphics;ImportJava.awt.Graphics2D;ImportJava.awt.image.BufferedImage;ImportJavax.swing.JComponent;ImportJavax.swing.JDialog; Public  class Imagegui extends jcomponent {    /**     *      */    Private Static Final LongSerialversionuid =1LPrivateBufferedImage image; Public Imagegui() {    }@Override    protected void paintcomponent(Graphics g) {graphics2d g2d = (graphics2d) g;if(Image = =NULL) {g2d.setpaint (color.black); G2d.fillrect (0,0, This. GetWidth (), This. GetHeight ()); }Else{G2d.drawimage (image,0,0, This. GetWidth (), This. GetHeight (),NULL); System.out.println ("Show frame ..."); }    } Public void Createwin(String title) {JDialog UI =NewJDialog ();        Ui.settitle (title); Ui.getcontentpane (). setlayout (NewBorderLayout ()); Ui.getcontentpane (). Add ( This, Borderlayout.center); Ui.setsize (NewDimension ( the, -)); Ui.setvisible (true); } Public void Createwin(String title, Dimension size) {JDialog UI =NewJDialog ();        Ui.settitle (title); Ui.getcontentpane (). setlayout (NewBorderLayout ()); Ui.getcontentpane (). Add ( This, Borderlayout.center);        Ui.setsize (size); Ui.setvisible (true); } Public void Imshow(BufferedImage image) { This. image = Image; This. repaint (); }}

Pay attention to the public number, get more knowledge of image processing and OPENCV-related knowledge
"Gloomyfish-Jia Zhigang"

Java uses OpenCV3.2 for video reading and playback

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.