How to connect DVR camera in Java? [closed]

6

I have a question, I have searched the internet but I do not think anything consistent: how to connect to a DVR camera (not IP camera) using Java?

I already use OpenCV and can connect to IP camera. Is it possible to connect OpenCV with DVR?

Below is a code that I found in StackOverFlow in English and adapted. However, the while((bytesRead = s_in.read()) > 0) line does not print any response. So I can not render any images on the screen.

public class ConectaDVR {

    Socket s = new Socket();

    public void conecta() throws Exception {
        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                System.out.println("Authenticatting...");
                PasswordAuthentication p = new PasswordAuthentication("admin", "123456".toCharArray());
                return p;
            }
        });

        String host = "187.45.142.191"; //192.168.80.107
        PrintWriter s_out = null;
        BufferedReader s_in = null;

        try {
            s.connect(new InetSocketAddress(host, 9000));
            System.out.println("Is connected? : " + s.isConnected());

            s_out = new PrintWriter(s.getOutputStream(), true);
            s_in = new BufferedReader(new InputStreamReader(s.getInputStream()));

        } catch (UnknownHostException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

        int bytesRead = 0;
        System.out.println("Reading... \n");
        System.out.println();
        while ((bytesRead = s_in.read()) > 0) {
            System.out.println(s_in.readLine());
        }
        System.out.println("Done");
    }

    public BufferedImage getBufferedImage() {

        BufferedImage bufImage = null;
        try {

            InputStream in = new ByteArrayInputStream(IOUtils.toByteArray(s.getInputStream()));
            bufImage = ImageIO.read(in);
        } catch (Exception ext) {
            ext.printStackTrace();;
        }
        BufferedImage bi = bufImage;
        ImageIcon ii = null;
        ii = new ImageIcon(bi);
        Image newimg = bi.getScaledInstance(320, 220, java.awt.Image.SCALE_SMOOTH);
        ii = new ImageIcon(newimg);
        Image i2 = ii.getImage();
        bufImage = new BufferedImage(i2.getWidth(null), i2.getHeight(null), BufferedImage.SCALE_SMOOTH);
        bufImage.getGraphics().drawImage(i2, 0, 0, null);
        return bufImage;
    }

}
    
asked by anonymous 04.12.2014 / 14:45

1 answer

-1

Yes you can connect OpenCV with the DVR camera in Java.

Here is a sample code that is working to connect:

package opencvdemo;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;

public class VideoStream
{
    public static void main(String args[]) throws InterruptedException
    {
        System.out.println("opencv start..");

        // Load native library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        VideoCapture camView=new VideoCapture();

        camView.open(0);

        if(!camView.isOpened())
        {
            System.out.println("Camera Error..");
        }
        else
        {
            System.out.println("Camera successfully opened");
        }

        videoCamera cam=new videoCamera(camView);

        //Initialize swing components
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.add(cam);
        frame.setSize(1080,720);  
        frame.setVisible(true);

        while(camView.isOpened())
        {
            cam.repaint();
        }
    }        
}

@SuppressWarnings("serial")
        class videoCamera extends JPanel
{
    VideoCapture camera; 

    public videoCamera(VideoCapture cam) 
    {
        camera  = cam; 
    }

    public BufferedImage Mat2BufferedImage(Mat m)
    {
        int type = BufferedImage.TYPE_BYTE_GRAY;
        if (m.channels() > 1)
        {
            type = BufferedImage.TYPE_3BYTE_BGR;
        }
        int bufferSize = m.channels() * m.cols() * m.rows();
        byte[] b = new byte[bufferSize];
        m.get(0, 0, b); // get all the pixels
        BufferedImage img = new BufferedImage(m.cols(), m.rows(), type);
        final byte[] targetPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
        System.arraycopy(b, 0, targetPixels, 0, b.length);
        return img;
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Mat mat = new Mat();

        if( camera.read(mat))
        {
            System.out.print("IMAGE");
        }

        BufferedImage image = Mat2BufferedImage(mat);

        g.drawImage(image,10,10,image.getWidth(),image.getHeight(), null);
    }
    public Mat turnGray( Mat img)
    {
        Mat mat1 = new Mat();
        Imgproc.cvtColor(img, mat1, Imgproc.COLOR_RGB2GRAY);
        return mat1;
    }
    public Mat threash(Mat img)
    {
        Mat threshed = new Mat();
        int SENSITIVITY_VALUE = 100;
        Imgproc.threshold(img, threshed, SENSITIVITY_VALUE,255,Imgproc.THRESH_BINARY);
        return threshed;
    }
}

If you see the error "Camera error ..." , there is usually a workaround for these two options:

1st Download the VLCj library in this link and put it in your project and run it.

opencv_ffmpeg.dll in "c: \ opencv \ build \ x86 \ vc10 \ bin"

Also take a look at open-source DVR projects for Java in this link .

    
16.06.2016 / 06:53