i'm trying make webcam
example in java doesn't work. repaint method jpanel
doesn't call paintcomponent
. when call repaint anywhere, doesn't update image program still keeps running:
this example:
public class jframeexample extends jframe { private static final long serialversionuid = 1l; private jpanel contentpane; private camerapanel camerapanel; /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { jframeexample frame = new jframeexample(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create frame. */ public jframeexample() { setdefaultcloseoperation(jframe.exit_on_close); setbounds(300, 200, 900, 600); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); contentpane.setlayout(null); camerapanel = new camerapanel(); camerapanel.setbackground(color.red); camerapanel.setbounds(10, 52, 640, 480); contentpane.add(camerapanel); jbutton btnactivar = new jbutton("activar"); btnactivar.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { startcamera(); } }); btnactivar.setbounds(10, 11, 89, 23); contentpane.add(btnactivar); } private void startcamera(){ system.loadlibrary(core.native_library_name); videocapture eyecamera = new videocapture(0); if(eyecamera.isopened()){ while(true){ mat frame = new mat(); eyecamera.read(frame); camerapanel.setimage(mattobufferedimage(frame)); camerapanel.setsize(new dimension(frame.width(),frame.height())); contentpane.repaint(); camerapanel.repaint(); this.repaint(); } } } public static bufferedimage mattobufferedimage(mat matrix) { int cols = matrix.cols(); int rows = matrix.rows(); int elemsize = (int) matrix.elemsize(); byte[] data = new byte[cols * rows * elemsize]; int type; matrix.get(0, 0, data); switch (matrix.channels()) { case 1: type = bufferedimage.type_byte_gray; break; case 3: type = bufferedimage.type_3byte_bgr; // bgr rgb byte b; (int = 0; < data.length; = + 3) { b = data[i]; data[i] = data[i + 2]; data[i + 2] = b; } break; default: return null; } bufferedimage image2 = new bufferedimage(cols, rows, type); image2.getraster().setdataelements(0, 0, cols, rows, data); return image2; } }
camerapanel
public class camerapanel extends jpanel{ private static final long serialversionuid = 1l; private bufferedimage image; public camerapanel() { super(); } public bufferedimage getimage() { return image; } public void setimage(bufferedimage newimage) { image = newimage; system.out.println("setimage method"); } @override protected void paintcomponent(graphics grafics) { system.out.println("paintcomponent method"); super.paintcomponent(grafics); if(image != null) grafics.drawimage(image, 10, 10, 50, 50, this); } }
i not try because app not runnable. maybe runnig while(true) {}
part in thread
can solve problem.
thread paintthread = new thread(new runnable(){ public void run() { while(true){ mat frame = new mat(); eyecamera.read(frame); camerapanel.setimage(mattobufferedimage(frame)); camerapanel.setsize(new dimension(frame.width(),frame.height())); contentpane.repaint(); camerapanel.repaint(); this.repaint(); } } } paintthread.start();
Comments
Post a Comment