gpt4 book ai didi

java - 如何处理sarxos Java网络摄像头的网络摄像头拔出然后重新插入?

转载 作者:行者123 更新时间:2023-12-03 13:06:13 25 4
gpt4 key购买 nike

我试图使用sarxos网络摄像头库创建一个单例类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( "" );
}
}
我在主类中为网络摄像头生成了一个新线程,如下所示:
Thread webcamThread = new Thread( WebCamFrame.getInstance() );
webcamThread.start();
try {
webcamThread.join();
} catch ( InterruptedException ex ) {
Logger.getLogger( tsInfluence.class.getName() ).log( Level.SEVERE, null, ex );
}
这是我第一次尝试实现单例类,我觉得这可能是我的问题所在,对于任何错误,我们深表歉意。谢谢

最佳答案

评论中的@silentsudo建议对我有用:
“我建议创建一个单独的类来直接处理网络摄像头事件,然后您可以创建自己的监听器以提供对UI类的回调”

关于java - 如何处理sarxos Java网络摄像头的网络摄像头拔出然后重新插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66485557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com