我正在尝试编写一个用网络摄像头拍摄照片的程序。我想将JButton添加到实时视频的JFrame中。然而,按钮没有显示出来。我的信念是它被视频覆盖。我在下面附上了我的代码。
def as_json(options = {})
super(include: :user)
end
任何建议都很棒 我看了很多关于Jbuttons没有出现的问题,但我不确定哪一个跟我的相似。如果有的话,请指出正确的方向。
提前谢谢。
答案 0 :(得分:3)
您正在向内容窗格添加按钮,然后在其上绘制视频。当然视频会覆盖您的按钮。
一些建议:
请勿设置自定义内容窗格。
创建两个单独的面板并将其添加到内容窗格。
将按钮添加到一个面板。
将您的视频制作到另一个视频。
重构代码以正确覆盖paintComponent
并从另一个帖子重绘。
答案 1 :(得分:2)
我不确定您使用哪个库来创建所述视频。虽然在此示例中,我使用opencv
库从网络摄像头创建图像。您必须对其进行修改,以使其适用于您的用例。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.opencv.core.*;
import org.opencv.videoio.VideoCapture;
public class ApplicationBase {
private BufferedImage m_Captured_BufferedImage;
static {
System.out.println(System.getProperty("java.library.path"));
System.loadLibrary ( Core.NATIVE_LIBRARY_NAME );
}
private void displayGUI ( BufferedImage image, String title, int x, int y ) {
JFrame frame = new JFrame ( title );
frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );
JPanel contentPane = new JPanel ( new BorderLayout ( 5, 5 ) );
PictureDrawer pictureDrawer = new PictureDrawer ( image );
contentPane.add ( pictureDrawer, BorderLayout.CENTER );
JButton button = new JButton ( "Click Me not!" );
contentPane.add ( button, BorderLayout.PAGE_END );
frame.setContentPane ( contentPane );
frame.pack ();
frame.setLocationByPlatform ( true );
frame.setVisible ( true );
}
private void startApp () {
VideoCapture camera = new VideoCapture ( 0 );
Mat videoFrame = new Mat ();
camera.read ( videoFrame );
if ( camera.read ( videoFrame ) ) {
m_Captured_BufferedImage = matToBufferedImage ( videoFrame );
Runnable runnable = new Runnable () {
@Override
public void run () {
displayGUI ( m_Captured_BufferedImage, "Image", 0, 0 );
}
};
EventQueue.invokeLater ( runnable );
}
camera.release ();
}
public BufferedImage matToBufferedImage ( Mat videoFrame ) {
//Mat() to BufferedImage
int type = 0;
if ( videoFrame.channels () == 1 ) {
type = BufferedImage.TYPE_BYTE_GRAY;
} else if ( videoFrame.channels () == 3 ) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
BufferedImage image = new BufferedImage ( videoFrame.width (), videoFrame.height (), type );
WritableRaster raster = image.getRaster ();
DataBufferByte dataBuffer = ( DataBufferByte ) raster.getDataBuffer ();
byte [] data = dataBuffer.getData ();
videoFrame.get ( 0, 0, data );
return image;
}
public static void main ( String [] args ) {
new ApplicationBase ().startApp ();
}
}
class PictureDrawer extends JComponent {
private BufferedImage m_BufferedImage;
public PictureDrawer ( BufferedImage bufferedImage ) {
this.m_BufferedImage = bufferedImage;
}
@Override
protected void paintComponent ( Graphics g ) {
super.paintComponent ( g );
g.drawImage ( m_BufferedImage, 0, 0, this );
}
@Override
public Dimension getPreferredSize () {
return new Dimension ( m_BufferedImage.getWidth (), m_BufferedImage.getHeight () );
}
}
我从this
下载了OpenCV库使用以下命令编译项目:
javac -classpath .;C:\install\opencv\opencv\build\java\opencv-320.jar; -d bin source\*.java
运行项目的命令:
>java -classpath .;C:\install\opencv\opencv\build\java\opencv-320.jar; -Djava.library.path=C:\install\opencv\opencv\build\java\x64 ApplicationBase