Hi there!
I’ve been developing my URCap. My main goal is to display the actual image of the webcam/camera through sockets. I’ve created a ByteArrayOutputStream to send the image captured from my cam. This ByteArray is read in the URCap, and then it “set” a label that contains the image.
My problem is that I’ve tried and tested this socket in the VM and works perfectly. It receives the image every second, set the label and then I refresh it in NodeView, in order to show the actual image.
But it doesn’t work in the real UR. I’ve tried to modify the refresh and the timer that executes the input image, but doesn’t work. Any idea? I received a pixeled image.
This is part of the code to receive the image:
// Se recibe la imagen mediante el input de datastream y se escribe en el directorio
byte[] imagenrecibida = new byte[62100];
in.read(imagenrecibida);
System.out.println("Stream recibido");
BufferedImage imagen = ImageIO.read(new ByteArrayInputStream(imagenrecibida));
System.out.println("Imagen guardada como BufferedImage");
in.close();
return imagen;
Code in NodeView to refresh the label:
private void refreshPanelLabel(final JLabel label) {
timer = new Timer(0, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
label.repaint();
}
});
timer.setRepeats(true);
// Refresca el panel a 60 FPS
timer.setDelay(1000);
timer.start();
}
TIMER TO EXECUTE THE RUTINE TO RECEIVED AND SET THE IMAGE
public void onStartClick() {
// RUTINA QUE SE EJECUTA AL PULSAR BOTÓN START
// EL CLIENTE (ROBOT) SE CONECTA AL SERVIDOR (PC) PARA RECIBIR LA IMAGEN CONTINUAMENTE
view.visiblestopbutton(true);
view.habilitarstopbutton(true);
view.habilitarstartbutton(false);
view.setConnectCorrect();
uiTimer = new Timer(true);
uiTimer.schedule(new TimerTask() {
@Override
public void run() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
// Conexión válida será true siempre y cuando se mantenga la conexión. Si salta el timeout, para
enviarthreshold.sendNow(getIpAddress(), getThreshold(),getOtsuact(),getErode(),getDilate());
BufferedImage image = obtenerimagen.readNow(getIpAddress()); // Se establece la conexión y devuelve la imagen del socket
view.setIcono(image);
}
});
}
}, 0, 1500); // Every 1.5 seconds received and set the image
}