Results 1 to 5 of 5
  1. #1
    katie is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default how to save image as jpg?

    I have made an application that reads a jpg image, manipulates it like a filter and exports the new data array to a MemoryImageSource and then to an Image.

    How can I save this Image or MemoryImageSource to a legal image file such as Bmp, Jpg?
    thanks in advance

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.ImageIO;
    
    public class SavingAnImage {
    
        public static void main(String[] args) throws IOException {
            String path = "images/cougar.jpg";
            BufferedImage src = ImageIO.read(new File(path));
            // Convert Image to BufferedImage if required.
            BufferedImage image = toBufferedImage(src);
            save(image, "jpg");  // png okay, j2se 1.4+
            save(image, "bmp");  // j2se 1.5+
                                 // gif okay in j2se 1.6+
        }
    
        private static void save(BufferedImage image, String ext) {
            String fileName = "savingAnImage";
            File file = new File(fileName + "." + ext);
            try {
                ImageIO.write(image, ext, file);  // ignore returned boolean
            } catch(IOException e) {
                System.out.println("Write error for " + file.getPath() +
                                   ": " + e.getMessage());
            }
        }
    
        private static BufferedImage toBufferedImage(Image src) {
            int w = src.getWidth(null);
            int h = src.getHeight(null);
            int type = BufferedImage.TYPE_INT_RGB;  // other options
            BufferedImage dest = new BufferedImage(w, h, type);
            Graphics2D g2 = dest.createGraphics();
            g2.drawImage(src, 0, 0, null);
            g2.dispose();
            return dest;
        }
    }

  3. #3
    katie is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default

    you are very fast
    thanks for everything

  4. #4
    neerajvenator is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Post BufferToImage

    Sir we would like you to guide us how to grab a frame from video and then save that image so that we can access that image and do the required analysis and proccessing.
    We have been using FrameAcccess and have also tried modifying the accessframe function but we are getting the following error :-
    void accessFrame(Buffer frame) {

    FrameGrabbingControl fgc = (FrameGrabbingControl)p.getControl("javax.media.co ntrol.FrameGrabbingControl");
    buf = fgc.grabFrame();

    int x = frame.FLAG_NO_SYNC;

    // Convert it to an image
    btoi = new BufferToImage((VideoFormat)buf.getFormat());
    img = btoi.createImage(buf);

    if (img != null)
    {
    saveJPG(img,"d:\\Movies\\Musicvids\\drp\\dr" ,frame.getSequenceNumber() ".jpg");
    }

    long t = (long)(frame.getTimeStamp()/10000000f);
    System.out.println("Pre: frame #: " frame.getSequenceNumber() ", time: " ((float)t)/100f ", len: " + frame.getLength());

    }

    but we are not able to modify the path.....

    kindly help us at your earliest....

    thanks in advance....

  5. #5
    elizas is offline Member
    Join Date
    Jan 2010
    Location
    India
    Posts
    4
    Rep Power
    0

    Default Converting text to image in java awt

    This tip describes how text can be converted to image using Java awt..

    Step1: Create a String which we will convert to image
    Step2: Create a BufferedImage
    Step3: Create get the height and width of the String
    Step4: call the createGraphics(), which draws the BufferedImage object to Graphics2D object
    Step6: set the font color, backgroung color etc as required

    Any suggestions are appreciated.

Similar Threads

  1. How can i save the data Internally(auto save)
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2010, 08:31 PM
  2. Writing text into an image and save it
    By elcapi in forum Java 2D
    Replies: 6
    Last Post: 09-18-2009, 05:47 PM
  3. how to save image to disk after using pixelgrabber
    By shishirg in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 02:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •