import java.awt.*; import java.awt.event.*; import java.io.File; import javax.swing.*; import java.awt.image.*; import java.util.Vector; import javax.swing.*; import javax.swing.border.*; import java.io.*; import javax.imageio.ImageIO; class Jcv { // -----Definitions public static final int DEF_MATCHER=1, DEF_MATCHER_NAME=2; //Def funcs public static native int defIs(int index, int type); //returns 0 or 1 public static native int defCreateAt(int index, int what); // ------Global public static final int G_LIB=101, G_PDB=102, G_IMAGE=103, G_ID=104, G_POLYGON=105, G_STRING=106, G_ANIM=107, G_ARRAY=108, G_RECT=109, G_GRAPHICS=110; //Global functions public static native int gCreate(int type); //-1 for failure public static native void gRelease(int index); //Releases the current one, -1 for failure public static native int gIs(int index, int type); //0 or 1 public static native int gGeti(int what); //-1 for failure public static native void gSeti(int value, int what); //remember to set them before using! // ------Object funcs public static native int geti(int obj, int oWhat); public static native void seti(int obj, int value, int oWhat); public static native float getf(int obj, int oWhat); public static native void setf(int obj, float value, int oWhat); protected static native int getsLow(int obj, byte buf[], int what); public static byte[] gets(int obj, int what){ int s=Jcv.getsLow(obj, null, what); if(s<0) return null; byte buf[]=new byte[s]; if(Jcv.getsLow(obj, buf, what)==s) return buf; else return null; } public static native void sets(int obj, byte c[], int oWhat); protected static native int getpLow(int obj, byte dst[], int what); //-1 on failure public static byte[] getp(int obj, int what){ //built on top of getp int s=getpLow(obj, null, what); if(s<0) return null; byte buf[]=new byte[s]; if(getpLow(obj, buf, what)==s) return buf; else return null; } public static native int refer(int obj, int oWhat); public static native int action(int obj, int oWhat); //-1 on failure public static native int actioni(int obj, int param, int oWhat); //-1 on failure public static native int actions(int obj, byte[] param, int oWhat); //-1 on failure public static native int is(int obj, int index, int oWhat); public static native int getAti(int obj, int index, int oWhat); public static native void setAti(int obj, int index, int value, int oWhat); public static native float getAtf(int obj, int index, int oWhat); public static native void setAtf(int obj, int index, float value, int oWhat); public static native int referAt(int obj, int index, int oWhat); public static native int create(int obj, int oWhat); public static native int createAt(int obj, int index, int oWhat); public static native int actionii(int obj, int param1, int param2, int oWhat); // ----------lib section //Matcher types public static final int CONVEXITY=1, CIRCULARITY=2, ECCENTRICITY=3, FOURIER_COMPLEX=4, FOURIER_CURVATURE=5, FOURIER_CENTRAL=6, TFD=7, CSS=8; //other public static final int MATCHER=310, PDB=311, MATCHERS_NUM=312, RESULTS_NUM=313, MATCHER_TYPE=314, RESULT=315, POLYGON=316, SCORE=317, MATCHED=318, PUSH_MATCHER_TYPE=319, POP_MATCHER=320, FIND_MATCHER_TYPE=321, QUERY=322, MATCH=323, MATCH_IMAGE=324, MATCH_ANIM=325, EXTERNAL_QUERY=326, RESULTS=327, DESCRIPTORS_NUM=328; // ---------PolygonDB section public static final int P_IMAGE=401, P_POLYGON=402, P_SAMPLED_POLYGON=403, P_LOADIMAGES=404, P_IMAGE_NAME=405, P_PATH=406, P_POLYGONS_NUM=407, P_LOAD=408, P_UNLOAD=409, P_WRITE=410; // ---------Image section public static final int IM_SIZE=501, IM_WIDTH=502, IM_HEIGHT=503, IM_DATA=504, IM_WRITE=505, IM_LOAD=506, IM_EXTRACT_OUTLINE=507, IM_ERODE=508, IM_DILATE=509, IM_COPY=510, IM_THRESHOLD=511, GCV_IM_RECT_COPY=512, GCV_IM_RESIZE=513, IM_GRAPHICS=514, GCV_IM_ZERO=515; public static BufferedImage imGetImage(int image){ int w=geti(image, IM_WIDTH); int h=geti(image, IM_HEIGHT); int size; byte buf[]=gets(image, IM_DATA); if(w<=0 || h<=0 || buf==null) return null; BufferedImage bim=new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); int lineSize=w*3; if((lineSize%4)!=0) lineSize+=( 4-(lineSize%4) ); for(int i=0; i=0) image=jcv.imGetImage(gim); else image=null; //validate(); str="Frame "+fr+": "; String desc=jcv.newStr(jcv.getp(anim, Jcv.ANIM_FRAME_DESCRIPTION)); if(desc!=null) str+=desc; paint(g); try{ /*String wr[] =ImageIO.getWriterFormatNames(); for(int i=0; i0){ Polygon p=jcv.polGetPolygon(drawPol); if(p!=null) g.drawPolygon(p); } jcv.gRelease(pol); } } } }; class MessageDialog extends JDialog implements ActionListener{ public JLabel lab=new JLabel(""); public JButton but=new JButton("OK"); public MessageDialog(Frame parent, String text){ super(parent, true); Container p=getContentPane(); p.setLayout(new GridLayout(2, 1)); setLabText(text); p.add(S.inPanel(lab)); but.addActionListener(this); p.add(S.inPanel(but)); Dimension dim=getPreferredSize(); dim.width+=50; dim.height+=50; setSize(dim); Dimension sdim=Toolkit.getDefaultToolkit().getScreenSize(); setLocation((sdim.width-dim.width)/2, (sdim.height-dim.height)/2); } public void setLabText(String s){ lab.setText(s); validate(); } public void actionPerformed(ActionEvent ae){ setVisible(false); } }; class PolygonPanel extends JPanel{ public Polygon pol=null; public String str=null; public void paint(Graphics g){ super.paint(g); g.setColor(Color.RED); if(pol!=null) g.drawPolygon(pol); if(str!=null) g.drawString(str, 0, 0); } }; class RCommandPanel extends JPanel{ public Button matchBut, viewMatchBut, viewAnimBut; public RCommandPanel(){ setLayout(new GridLayout(1, 3)); matchBut=new Button("Match"); add(S.inPanel(matchBut)); viewMatchBut=new Button("View Match"); add(S.inPanel(viewMatchBut)); viewAnimBut=new Button("View Match Animation"); add(S.inPanel(viewAnimBut)); } }; class RMatchViewFrame extends JFrame{ ImagePanel imagePanel=new ImagePanel(); public RMatchViewFrame(){ setTitle("Match Visualization"); Container pane=getContentPane(); pane.setLayout(new BorderLayout()); pane.add(imagePanel, "Center"); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ setVisible(false); }}); } public void setImage(Image im){ imagePanel.image=im; setVisible(true); imagePanel.repaint(); } }; class RScrollPanel extends JPanel{ public JScrollBar bar; public PolygonPanel polygonPanel; public JLabel lab, lab2; public RScrollPanel(String title){ bar=new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 100); bar.setUnitIncrement(1); bar.setBlockIncrement(10); bar.setEnabled(true); polygonPanel=new PolygonPanel(); polygonPanel.setBorder(new BevelBorder(BevelBorder.LOWERED)); lab=new JLabel(title); lab2=new JLabel(""); JPanel p=new JPanel(); p.setLayout(new GridLayout(3,1)); p.add(S.inPanel(lab)); p.add(S.inPanel(lab2)); p.add(S.inPanel(bar)); p.setBorder(new BevelBorder(BevelBorder.LOWERED)); setLayout(new BorderLayout()); add(p, "West"); add(polygonPanel, "Center"); setScrollVals(0, 0); } public void setLabVals(int min, int current, int max){ lab2.setText(""+min+":"+current+":"+max); revalidate(); } public void setScrollVals(int current, int max){ bar.setMinimum(0); int m=Math.max(max, current+1); bar.setMaximum(m); bar.setValue(current); if(m==max) bar.setEnabled(true); else bar.setEnabled(false); setLabVals(0, current, m); } public void setPolygon(Polygon pol){ polygonPanel.pol=pol; polygonPanel.repaint(); } public void setPolygonDescription(String s){ polygonPanel.str=s; polygonPanel.repaint(); } }; class S{ public static JPanel inPanel(Component c){ JPanel p=new JPanel(); p.add(c); return p; } public static Polygon invertY(Polygon src){ Polygon p=new Polygon(); Rectangle r=src.getBounds(); //int ym=r.y+r.height/2; int ys=r.y+r.height; int s=src.npoints; for(int i=0; i0) p=getPolygon(results[0]); downPan.setPolygon(p); } repaint(); return; } if(but!=null && but.getLabel().equals("View Match")){ int query=upPan.bar.getValue(); int res1=downPan.bar.getValue(); if(results==null || jcv.is(lib, query, jcv.POLYGON)==0 || res1>=results.length || jcv.is(lib, results[res1], Jcv.POLYGON)==0) return; BufferedImage bim; int im=jcv.createAt(lib, results[res1], Jcv.MATCH_IMAGE); if(im>=0){ bim=jcv.imGetImage(im); jcv.gRelease(im); }else{ System.out.println("Could not create image"); bim=null; } //System.out.println("bim="+bim); matchViewFrame.setImage(bim); return; } if(but!=null && but.getLabel().equals("View Match Animation")){ //dout("manim pressed"); int query=upPan.bar.getValue(); int res1=downPan.bar.getValue(); if(results==null || jcv.is(lib, query, jcv.POLYGON)==0 || res1>=results.length || jcv.is(lib, results[res1], Jcv.POLYGON)==0){ new MessageDialog(this, "Unable to create animation object").setVisible(true); return; } //dout("passed checks"); int anim=jcv.createAt(lib, results[res1], Jcv.MATCH_ANIM); //dout("anim="+anim); if(anim<0){ matchersArea.setText("This matcher does not support this feature!"); compFrame.setComponent(matchersArea); compFrame.setVisible(true); return; } JcvAnimator animator=new JcvAnimator(jcv, anim); setVisible(false); matchViewFrame.setVisible(false); compFrame.setComponent(animator); compFrame.setSize(500, 400); compFrame.setVisible(true); Thread t=new Thread(animator); t.start(); try{ t.join(); }catch(Exception e){ e.printStackTrace(System.out); } compFrame.setVisible(false); setVisible(true); jcv.gRelease(anim); } //otherwise, a pushMatcher command! int s=definedMatchers.size(); for(int i=0; iresults.length) pol=null; else pol=getPolygon(results[index]); str=""+jcv.getAtf(lib, results[index], Jcv.SCORE); } p.setLabVals(0, index, sb.getMaximum()); p.setPolygon(pol); p.setPolygonDescription(str); } public Polygon getPolygon(int index){ Polygon pol=null; int _pol=jcv.createAt(pdb, index, Jcv.P_POLYGON); if(_pol>=0){ pol=jcv.polGetPolygon(_pol); jcv.gRelease(_pol); //if(pol==null) return null; //pol=S.invertY(pol); } return pol; } public void viewMatchers(){ int s=jcv.geti(lib, Jcv.MATCHERS_NUM); StringBuffer buf=new StringBuffer(); buf.append("Matchers stack, size=").append(s).append("\n"); for(int i=0; i