Search code examples
javamultithreadingsingletonqr-codewebcam

How to handle webcam unplug then replug for sarxos java webcam?


I have tried to create a singleton class WebCamFrame to create a webcam using the sarxos webcam library. I am trying to handle the case where my webcam is disconnected then reconnected. I have tried to handle this case but my webcam frame shows 'no image available' text when i reopen the webcam frame. Is my attempt to handle this case correct?

My webcamframe class:

/**
 * WebCam Frame which is a singleton class that runs on a new thread
 * It reads in video stream from webcam and attempts to read a qr code
 */
public class WebCamFrame extends JFrame implements Runnable, WebcamDiscoveryListener {
    private static WebCamFrame singleton;
    private Webcam webcam = null;
    private WebcamPanel panel = null;
    private JLabel playedCardLabel = null;
    private JButton confirmButton = null;
    private boolean running;
    
    public static WebCamFrame getInstance() {
        if( singleton == null ) 
            singleton = new WebCamFrame();
        return singleton;
    }
    
    @Override
    public void webcamFound( WebcamDiscoveryEvent event ) {
        if( webcam == null ) return;
        Webcam newWebcam = event.getWebcam();
        if( newWebcam.getName().equals( webcam.getName()) ) {
            close();
            webcam = Webcam.getWebcamByName( webcam.getName() );
            webcam.open();
            panel = new WebcamPanel( webcam );
        }
    }

    @Override
    public void webcamGone( WebcamDiscoveryEvent event ) {
        if( webcam == null ) return;
        Webcam disconnectedWebCam = event.getWebcam();
        if( disconnectedWebCam.getName().equals( webcam.getName()) ) {
            playedCardLabel.setText( "WebCam disconnected, please reconnect." );
            webcam.close();
        }
    }
    
    private WebCamFrame() {
        super();
        Webcam.addDiscoveryListener( this );
        running = true;
        setLayout( new FlowLayout() );
        setTitle( "Scan a card to make your move" );
        setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE );
        addWindowListener( new WindowAdapter() {
            @Override
            public void windowClosing( WindowEvent e ) {
                super.windowClosing( e );
                close();
            }
        } );
        Dimension size = WebcamResolution.QVGA.getSize();
        webcam = Webcam.getWebcams().get( 1 );
        webcam.setViewSize( size );
        panel = new WebcamPanel( webcam );
        panel.setPreferredSize( size );
        panel.setFPSDisplayed( true );
        panel.setLayout( new BorderLayout() );
        playedCardLabel = new JLabel( "", SwingConstants.CENTER );
        if( Webcam.getDefault() == null ) playedCardLabel.setText( "No Webcam detected" );
        confirmButton = new JButton( "Confirm Selection" );
        confirmButton.setVisible( false );
        confirmButton.addActionListener( e -> {
          close();
        } );
        add( panel );
        JPanel subPanel = new JPanel();
        subPanel.setLayout( new BorderLayout() );

        subPanel.add( playedCardLabel, BorderLayout.NORTH );
        subPanel.add( confirmButton, BorderLayout.SOUTH );
        panel.add( subPanel, BorderLayout.SOUTH );
        pack();
        setVisible( false );
    }

    @Override
    public void run() {
        do {
            try {
                if( !running ) return;
                Thread.sleep( 100 );
            } catch( InterruptedException e ) {
                e.printStackTrace();
            }
            Result result = null;
            BufferedImage image;
            // Only try to read qr code if webcam is open and frame is webCamFrame is visible
            if( webcam.isOpen() && isVisible() ) {
                System.out.println("Entering");
                if( (image = webcam.getImage()) == null ) {
                    continue;
                }
                LuminanceSource source = new BufferedImageLuminanceSource( image );
                BinaryBitmap bitmap = new BinaryBitmap( new HybridBinarizer(source) );
                try {
                    result = new MultiFormatReader().decode( bitmap );
                } catch( NotFoundException e ) {
                    // fall through if there is no QR code in image
                }
            }
            if( result != null ) {
                playedCardLabel.setText( "You have played card " + result.getText() );
                confirmButton.setVisible( true );
            }
        } while( true );
    }

    /* Hide the webcam frame and reset its swing components */
    public void close() {
        setVisible( false );
        resetComponents();
    }

    public void open() {
        setVisible( true );
    }
    
    public void toggleOpen() {
        if( isVisible() )
            close();
        else 
            open();
    }
    
    // Kill the webcam, join the webcam thread and dispose of the webcam frame
    public void kill() throws InterruptedException {
        dispose();
        webcam.close();
        singleton = null;
        running = false;
    }

    private void resetComponents() {
        confirmButton.setVisible( false );
        playedCardLabel.setText( "" );
    }
}

I spawn a new thread for my webcam in my main class like so:

Thread webcamThread = new Thread( WebCamFrame.getInstance() );
webcamThread.start();
try {
            webcamThread.join();
        } catch ( InterruptedException ex ) {
            Logger.getLogger( tsInfluence.class.getName() ).log( Level.SEVERE, null, ex );
        }

This is my first time trying to implement a singleton class and I feel that is where my issue may lie, sorry in advance for any mistakes. Thanks


Solution

  • @silentsudo suggestion in the comments worked for me:

    "i suggest create a separate class to handle web cam event directly and then you can create your own listener to provide callbacks to UI class"