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;
}
}