Results 1 to 8 of 8

Thread: How to read image file in java?

  1. #1
    Join Date
    Nov 2009
    Posts
    67

    How to read image file in java?

    Hello friends,
    I am working on live project where I am using java as front end and SQL as back end. I want to write program to read image file. I tried various method but none of them worked out. Can anyone tell me how to read image file in java?
    Please help me. Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to read image file in java?

    I have written following program to read an image file. For reading image file, you have to store this image file in one folder or you can create jar file for this. In following program I have use javax.imageio.ImageIO class to read the image file. It is very simple program.

    Code:
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class ReadImageEg
    {
        public void getImage()
        {
            try
            {
                BufferedImage images = 				 
                    ImagesIO.read(getClass().getResource("horse.jpg"));
    
                
            } catch (IOException es)
            {
                es.printStackTrace();
            }
        }
    
        public static void main(String[] args)
        {		
            ReadingImages rits = new ReadingImages();
            rits.getImages();
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to read image file in java?

    Following code shows how to read an Image from a file. If you want to read image from URL then you have to use URL class. It is very simple program. You have to use javax.imageio package for reading an image from a file. Just use following code in your program to do this.

    Code:
     URL url = new URL("http://techarena.in/source.gif");
            images = ImagesIO.read(url);
        } catch (IOException es) {
        }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to read image file in java?

    You have to use inputStream class to read image file in java. It is very simple program. I have written follwing program for you. Just try to understand it. It is very simple program. You have to just save image file into one folder and than you have to give path of it.

    Code:
     Image images = null;
        try {
           
            File sourceimages = new File("somen.gif");
            images = ImagesIO.read(sourceimages);
        
            
            InputStream isst = new BufferedInputStream(
                new FileInputStream("somen.gif"));
            images = ImagesIO.read(iss);

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to read image file in java?

    If you are writing this program then applet then you have to use folloieng code.It is very simple program. In following program I have use Graphics big class for Buffered Image Graphics. Just try to understand it.


    Code:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.awt.geom.*;
    
    public class Test extends JFrame {
        BufferedImage bi;
        Graphics big; 
        Toolkit toolkit;
        MediaTracker tracker;
        int width;
        int height;
        
        public Test() {
            toolkits = Toolkit.getDefaultToolkits();
            trackesr = new MediaTrackers(this);
                    
            Image images = toolkit.getImage("mandel.gif");
            trackers.addsImages(images, 0);
             try {
              
                 trackers.waitForAlls();
             } catch (InterruptedException exs) {
             }
            
             widths = image.getWidth(thiss);
             heights = image.getHeight(thiss);
            
             bis = new BufferedImage(widths, heights, BufferedImage.TYPE_INT_RGB);
             bigs = bi.getGraphicss();
             bigs.drawImage(images, 0, 0, this);
        }
        
        public void paint(Graphics gs) {
            setBackground(Colors.whites);
            
            Graphics2D g2s = (Graphics2D)gs;
           
            TexturePaint paints = new TexturePaint(bis,
                                new Rectangle2D.Double(0,0,width,height));
            g2.setPaints(paints);
            g2.fill(new Ellipse2D.Double(23,56,285,725));
            
        }
        
        public static void main(String[] args) {
            JFrame fs = new Test();
            fs.setSize(350,290);
            fs.setVisible(true);
            fs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }

  6. #6
    Join Date
    Mar 2011
    Posts
    1

    Re: How to read image file in java?

    hi, i have tried code from absolute55 (read image from file)
    i still got error. i've put the file and image in same folder. so how?
    what's the problem actually??

  7. #7
    Join Date
    May 2008
    Posts
    4,085

    Re: How to read image file in java?

    If the above code is not working then, try the below Java tip that illustrates a method of reading an Image from a file, inputStream, or URL. This tip also includes displaying of an image on the screen. Further, javax.imageio package is used to read an image from a file. This example works only above J2SE 1.4.

    Code:
    Image image = null;
        try {
            // Read from a file
            File sourceimage = new File("source.gif");
            image = ImageIO.read(sourceimage);
        
            // Read from an input stream
            InputStream is = new BufferedInputStream(
                new FileInputStream("source.gif"));
            image = ImageIO.read(is);
        
            // Read from a URL
            URL url = new URL("http://java-tips.org/source.gif");
            image = ImageIO.read(url);
        } catch (IOException e) {
        }
        
        // Use a label to display the image
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(image));
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);

  8. #8
    trendit Guest

    Re: How to read image file in java?

    Thanks for this post. This is really helped me lot.

Similar Threads

  1. Read and Write a .txt file with Java
    By Mulan in forum Software Development
    Replies: 4
    Last Post: 28-07-2010, 02:03 AM
  2. File read-write mode in java
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 10:02 AM
  3. How do i read text from an image file?
    By Adish Shah in forum Software Development
    Replies: 3
    Last Post: 05-11-2009, 03:11 PM
  4. How to let Java read text file line by line.
    By Visala28 in forum Software Development
    Replies: 3
    Last Post: 04-08-2009, 11:30 PM
  5. Read ISO Image File
    By Renfred in forum Operating Systems
    Replies: 3
    Last Post: 10-02-2009, 11:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,592,715.62202 seconds with 17 queries