I used java swing to write an image tagging tool.

Source: Internet
Author: User
Tags cve

Function Description:

1. CLICK: select the cropping area.

2. Double Click: select the cropping area, save the cropping area as a file, and save the coordinates of the center of the area.

3. Open an image: Open the image from a folder and display it. At the same time, upload the paths of all the images in the directory. This function is mainly applicable to such situations,

If you process the image set of a folder and it is terminated or paused, the next time you start from the image. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + NCCxo7Tm1/release/CvLXEy/release + release/qsS/release + release/KjrLGjtObX + release/release + release/J0tTA + 9PDobC08r + release + examples/examples + cve-vcd4kpha + examples/ix6qGxus2hsM/examples + cve-vcd4kpha + z8LD5ri9yc/utlt6wuujuw.vcd4kpha + PGJyPgo8L3A + placement = "brush: java; "> import java. awt. borderLayout; import java. awt. color; import java. awt. container; import java. awt. dimension; import java. awt. flowLayout; import java. awt. graphics; import java. awt. gridLayout; import java. awt. image; import java. awt. toolkit; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. awt. event. keyEvent; import java. awt. event. keyListener; import java. awt. event. mouseAdapter; import java. awt. event. mouseEvent; import java. awt. image. bufferedImage; import java. awt. image. cropImageFilter; import java. awt. image. filteredImageSource; import java. awt. image. imageFilter; import java. io. bufferedWriter; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStreamWriter; import java. util. arrayList; import java. util. collections; import javax. imageio. imageIO; import javax. swing. JButton; import javax. swing. JFileChooser; import javax. swing. JFrame; import javax. swing. JPanel; import javax. swing. JScrollPane; import javax. swing. filechooser. fileNameExtensionFilter; public class PhotoFrame extends JFrame {private static final long serialVersionUID =-empty; private Container con; private MousePanel zPanel; private JScrollPane imgSp; private JPanel btnPanel; private String imageDir; private String currImg; // file name, excluding the directory private int currIndex; // ArrayList of the current image Index ImgList; // the absolute path public void writeCropImg () throws IOException {BufferedImage sourceImage = (BufferedImage) zPanel. getImg (); Image croppedImage; ImageFilter cropFilter; int x = zPanel. getPointX ()-25; int y = zPanel. getPointY ()-25; // The four parameters are the starting coordinate and width and height of the image, that is, CropImageFilter (int x, int y, int width, int height ), for details, see API // specify the width and height of the file to be cropped and the starting coordinate cropFilter = new CropImageFilter (x, y, 50, 50); // generate the image croppedImage = Toolkit. getdefatooltoolkit (). createImage (new FilteredImageSource (sourceImage. getSource (), cropFilter); // get the int h1 = croppedImage of the created image. getHeight (null); int w1 = croppedImage. getWidth (null); BufferedImage bi = new BufferedImage (w1, h1, BufferedImage. TYPE_INT_RGB); Graphics g = bi. getGraphics (); // you can set the background color g when drawing. drawImage (croppedImage, 0, 0, Color. white, null); String dir = imageDir + "_ crops"; // create a file output stream FileO UtputStream fos = new FileOutputStream (new File (dir + "/" + currImg); // write the created image to the output stream ImageIO. write (bi, "png", fos); fos. close ();} private void showNext () {updateCurrName (); zPanel. setImagePath (imgList. get (currIndex); currIndex ++;} private void updateCurrName () {String path = imgList. get (currIndex); int index = path. lastIndexOf ('/'); currImg = path. substring (index);} private class nextHandler implements Action Listener {@ Overridepublic void actionreceivmed (ActionEvent e) {showNext () ;}} private class openDirHandler implements ActionListener {@ Overridepublic void actionreceivmed (ActionEvent e) {fig = new JFileChooser (); chooser. setFileSelectionMode (JFileChooser. DIRECTORIES_ONLY); // you can only select the directory int returnVal = chooser. showOpenDialog (null); if (returnVal = JFileChooser. APPROVE_OPTION) {imageDir = choo Ser. getSelectedFile (). getPath (); imgList = FileTool. refreshFileList (imageDir); currIndex = 0; showNext (); // System. out. println ("the directory you selected is:" + selectPath); // The directory you selected is/home/linger/imdata/collar} private void savePointAndImg () {try {String dir = imageDir + "_ points"; int x = zPanel. getPointX (); int y = zPanel. getPointY (); // open the File f = new File (dir + "/"); if (! F. exists () {f. mkdir ();} f = new File (dir + "/" + currImg); if (! F. exists () {f. createNewFile ();} FileOutputStream fos = new FileOutputStream (f); OutputStreamWriter osw = new OutputStreamWriter (fos, "UTF-8"); BufferedWriter bw = new BufferedWriter (osw ); // write the bw file. write (x + "," + y); // close the bw file. close (); osw. close (); fos. close (); writeCropImg ();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace ();}} private class savePointHandler implements ActionListener {@ Overridepublic void actionreceivmed (ActionEvent e) {savePointAndImg () ;}} private class openImageHandler implements ActionListener {public void actionreceivmed (ActionEvent e) {JFileChooser chooser = new JFileChooser (); FileNameExtensionFilter filter = new FileNameExtensionFilter ("JPG & PNG Images", "jpg", "png"); chooser. setFileFilter (filter); int returnVal = chooser. showOpenDialog (null); if (returnVal = JFileChooser. APPROVE_OPTION) {File file = chooser. getSelectedFile (); imageDir = file. getParent (); imgList = FileTool. refreshFileList (imageDir); currImg = file. getName (); // file name, excluding the path // System. out. println (); String path = file. getAbsolutePath (); currIndex = imgList. indexOf (path); zPanel. setImagePath (path); currIndex ++; // internal class access external class, which can be accessed directly.} private PhotoFrame () {con = getContentPane (); con. setLayout (new GridLayout (2, 2); zPanel = new MousePanel (); // zPanel. setImagePath ("/home/linger/17820d01"); // bind an image // zPanel. setPreferredSize (new Dimension (zPanel. getImgWidth (), zPanel. getImgHeight (); imgSp = new JScrollPane (); imgSp. setPreferredSize (new Dimension (zPanel. getImgWidth (), zPanel. getImgHeight (); imgSp. setViewportView (zPanel); imgSp. setHorizontalScrollBarPolicy (JScrollPane. HORIZONTAL_SCROLLBAR_ALWAYS); imgSp. setVerticalScrollBarPolicy (JScrollPane. VERTICAL_SCROLLBAR_ALWAYS); // con. add (imgSp, BorderLayout. CENTER); con. add (imgSp); btnPanel = new JPanel (); btnPanel. setLayout (new FlowLayout (); btnPanel. setSize (300,100); JButton button = new JButton ("open image"); button. setSize (100, 50); btnPanel. add (button); button. addActionListener (new openImageHandler (); button = new JButton ("Save coordinates"); button. setSize (100, 50); btnPanel. add (button); button. addActionListener (new savePointHandler (); button = new JButton ("Open Directory"); button. setSize (100, 50); btnPanel. add (button); button. addActionListener (new openDirHandler (); button = new JButton ("Next"); button. setSize (100, 50); btnPanel. add (button); button. addActionListener (new nextHandler (); con. add (btnPanel); zPanel. addMouseListener (new MouseAdapter () {public void mouseClicked (MouseEvent e) {if (e. getClickCount () = 2) {savePointAndImg (); showNext () ;}}); finalSetting () ;}private void finalSetting () {setTitle ("annotation tool "); toolkit kit = Toolkit. getdefatooltoolkit (); Dimension screenSize = kit. getScreenSize (); int screenHeight = screenSize. height; int screenWidth = screenSize. width; int frameH = getHeight (); int frameW = getWidth (); setLocation (screenWidth-frameW)/2-250, (screenHeight-frameH)/2-250 ); setSize (800,600); // setSize (zPanel. getImgWidth () + 10, zPanel. getImgHeight () + 10); // setPreferredSize (new Dimension (zPanel. getImgWidth () + 100, zPanel. getImgHeight () + 100); // pack (); setVisible (true); setResizable (true); setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE);} public static void main (String [] args) {new PhotoFrame ();}}



Import java. awt. graphics; import java. awt. image; import java. awt. image. imageFilter; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; import javax. imageio. imageIO; public class ImgPanel extends javax. swing. JPanel {private static final long serialVersionUID = 1L; private Image image; private int imgWidth; private int imgHeight; public Image getImg () {return image;} Public int getImgWidth () {return imgWidth;} public void setImgWidth (int imgWidth) {this. imgWidth = imgWidth;} public int getImgHeight () {return imgHeight;} public void setImgHeight (int imgHeight) {this. imgHeight = imgHeight;} public ImgPanel () {} public void setImagePath (String imgPath) {// This method is not recommended. This method is used to load images without loading them to the memory, -1 is returned when the image width and height are obtained; // image = Toolkit. getdefatooltoolkit (). getImage (imgPath); try {// This method will The image is loaded into the memory to obtain the detailed information of the image. Image = ImageIO. read (new FileInputStream (imgPath); // read the setImgWidth (image. getWidth (this); setImgHeight (image. getHeight (this); this. repaint ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}@ Overridepublic void paintComponent (Graphics g1) {super. paintComponent (g1); int x = 0; int y = 0; Graphics g = (Graphics) g1; if (null = image) {return;} g. drawImage (image, x, y, image. getWidth (this), image. getHeight (this), this); g = null ;}}



Import java. awt. *; import javax. swing. *; import java. awt. event. *; public class MousePanel extends ImgPanel // JPanel {int x_pos, y_pos; public int getPointX () {return x_pos;} public int getPointY () {return y_pos;} public MousePanel () {addMouseListener (new MouseListener () {// mouseClicked (): Click public void mouseClicked (MouseEvent e) {x_pos = e. getX (); y_pos = e. getY (); repaint () ;}// mouseEntered (): public void mouseEntered (MouseEvent e) {}// mouseExited (): public void mouseExited (MouseEvent e) {}// mousePressed (): Press public void mousePressed (MouseEvent e) {}// mouseReleased (): public void mouseReleased (MouseEvent e) {}}); addMouseMotionListener (new MouseMotionListener () {public void mouseMoved (MouseEvent e) {} public void mouseDragged (MouseEvent e) {}});} public void paintComponent (Graphics g) {super. paintComponent (g); // g. drawString ("current location is: [" + x_pos + "," + y_pos + "]", x_pos, y_pos); // display on the Interface // System. out. printf ("current location is: [" + x_pos + "," + y_pos + "] \ n", x_pos, y_pos); // display g on the console. setColor (Color. RED); g. drawRect (x_pos-25, y_pos-25, 50, 50); // g. fillOval (x_pos, y_pos, 8, 8 );}}


import java.io.File;import java.util.ArrayList;public class FileTool {public static ArrayList
 
   refreshFileList(String strPath) { ArrayList
  
    filelist = new ArrayList
   
    ();         File dir = new File(strPath);         File[] files = dir.listFiles();         if (files == null)         {            return filelist;         }        for (int i = 0; i < files.length; i++)         {             if (files[i].isDirectory())             {                 refreshFileList(files[i].getAbsolutePath());             }             else            {                 String strFileName = files[i].getAbsolutePath().toLowerCase();                 //System.out.println(strFileName);                 filelist.add(files[i].getAbsolutePath());                                }         }        return filelist;}public static void main(String[] args) {// TODO Auto-generated method stubArrayList
    
       test = refreshFileList("/home/linger/imdata/collar");System.out.println(test.get(668));System.out.println(test.get(669));System.out.println(test.get(670));}}
    
   
  
 






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.