gpt4 book ai didi

java - 在 JavaCV 的 CanvasFrame 上显示一个组件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:15 25 4
gpt4 key购买 nike

这是我的程序,我需要在 Canvas 上添加一个按钮。如何实现?

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import org.opencv.objdetect.CascadeClassifier;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class CarDetect implements Runnable {
IplImage image;
static int count=0,imagecount=0;
static int label=0;
CanvasFrame canvas = new CanvasFrame("Detect");

public CarDetect() {

canvas.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
canvas.setBounds(200,0, 640,500);
**//canvas.add(any component such as panel,button etc)**
}

@Override
public void run() {

System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture camera = new VideoCapture(0);
// 1 for next camera

try {
Mat frame = new Mat();
while (true) {
camera.read(frame);

Mat image_tmp = frame;
MatOfByte matOfByte = new MatOfByte();
BufferedImage bufImage = null;
if(image_tmp!=null)
Highgui.imencode(".jpg", image_tmp, matOfByte);

byte[] byteArray = matOfByte.toArray();
try {
InputStream in = new ByteArrayInputStream(byteArray);
bufImage = ImageIO.read(in);
}
catch (Exception e) {
e.printStackTrace();
}
canvas.showImage(bufImage);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}


public static void main(String[] args) {
CarDetect gs = new CarDetect();
Thread th = new Thread(gs);
th.start();
}
}

最佳答案

import com.googlecode.javacv.CanvasFrame;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import net.miginfocom.swing.MigLayout;

public class Test {
BufferedImage bi;
CanvasFrame cf;
public Test() throws IOException{
bi = ImageIO.read(getClass().getResource("/resources/colorspec_.jpg"));
cf = new CanvasFrame("Canvas With Button");
cf.setLayout(new MigLayout("insets 0, gap 10", "grow", "grow"));
cf.add(new JButton("My Button"), "w 100:100:100, h 40:40:40");
}

public void showWindow(){
cf.showImage(bi);
cf.pack();
}

public static void main(String[] args) throws IOException {
Test t = new Test();
t.showWindow();
}
}

给你一些小提示:使用这个 MigLayout documentation 你可以玩得更多以满足您的要求..但是大多数时候我是通过 stackoverflow 找到它的高级用法的,不过……希望这有帮助.. =)

运行截图:

enter image description here

关于java - 在 JavaCV 的 CanvasFrame 上显示一个组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21718952/

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