BufferedImage convert to Image

Is there anyway to convert a BufferedImage into an Image. I've seen the other way around but not this way.
Any help would be greatly appreciated.

thank you.
See, I've got this method (below), that actually creates this image for me.
private static BufferedImage loadImage(final URL url) throws Exception{ //IOException {
     BufferedImage image;
         // Load the image through Image I/O
     image = ImageIO.read(url);
     //image = imageIO.read(url);
        // Check that the image was loaded and that we can get a GC
         if (image != null && !GraphicsEnvironment.isHeadless()) {
            final GraphicsConfiguration gc = GraphicsEnvironment
                    .getLocalGraphicsEnvironment()
                    .getDefaultScreenDevice()
                    .getDefaultConfiguration();
            // This probably can't be null if it's not headless
            // Not sure though so check anyway
            if (gc != null) {
                // Attempt to create compatible image with same transparency
                final BufferedImage img = gc.createCompatibleImage(
                        image.getWidth(), image.getHeight(),
                        image.getTransparency());
                // Check that img was created and isn't the same type
                // If it's the same type we don't need to convert obviously
                if (img != null && image.getType() != img.getType()) {
                    // Use ColorConvertOp to convert the rasters from one
                    // color space to the other
                    // This is faster than using Graphics to draw on the new
                    // image in many cases, sometimes 100x faster
                    ColorConvertOp op = new ColorConvertOp(
                            image.getColorModel().getColorSpace(),
                            img.getColorModel().getColorSpace(), null);
                    op.filter(image, img);
                    image = img;
         return image;
     }But, I need to call this from within the same class:
URL base;     
               try{
               base = new URL ("http://home.cogeco.ca/~someplace/folder");
               catch(Exception n)
                    System.out.println("Can't construct URL, first time around.");
               try{
               ll = loadImage(base); // right exception after calling method???
               catch(Exception n)
                    System.out.println("problems");
               }For some reason, I can't figure out if I'm handling the exception properly. Any ideas why I'm getting the following error?
load: tetrisapp.PictureData.class can't be instantiated.
java.lang.InstantiationException: tetrisapp.PictureData
     at java.lang.Class.newInstance0(Class.java:340)
     at java.lang.Class.newInstance(Class.java:308)
     at sun.applet.AppletPanel.createApplet(AppletPanel.java:778)
     at sun.applet.AppletPanel.runLoader(AppletPanel.java:707)
     at sun.applet.AppletPanel.run(AppletPanel.java:361)
     at java.lang.Thread.run(Thread.java:619)Tell me if you need more code. I'm guessing that the error is coming from the improper handling of the exception.
Message was edited by:
ManRed
Message was edited by:
ManRed

Similar Messages

  • How can BufferedImage convert to  Image?

    I want to use a BufferedImage to create a new ImageIcon, as ImageIcon(Image img), How to convert it to image without creating a new Image.
    Thanks.

    In other works, a BuffedImage is an Image.
    Kinda like a Ford is a Car. Of course, not necessarily that any Car is a Ford, neither is any Image necessarily a BufferedImage. But a BufferedImage is always an Image.

  • How can I convert an Image ( or BufferedImage ) to Base64 format ?

    Hi folks...
    How can I convert an Image, or BufferedImage, to the Base64 format ?
    The image that I want to convert, I get from the webCam connected to the computer...
    Anyone can help me ?
    Rodrigo Kerkhoff

    I suggest you read this thread concerning this:
    http://forum.java.sun.com/thread.jspa?forumID=31&threadID=477461
    Failing that, Google is your friend.
    Good luck!

  • Converting an Image object to a BufferedImage object?

    I have a BufferedImage object, bi, which I want to resize, hence:
    Image image = bi.getScaledInstance(400, 300, Image.SCALE_DEFAULT);
    Next I want to encode the resized image to JPEG format. The problem is that inorder to use the JPEGImageEncoder.encode() method, I need to pass it a BufferedImage object. I have looked through the API and cant see a way of converting the Image object to a BufferedImage object.
    Can anyone shed some light on this problem?
    Any help will be much appreciated.

    I copied + pasted the code from:
    http://java.sun.com/docs/books/tutorial/2d/problems/ind
    x.html :-)
    So you propably could use :
    int width = 400;
    int height = 300;
    Image image = bi.getScaledInstance(width, height,
    Image.SCALE_DEFAULT);
    BufferedImage bi = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);
    Graphics2D biContext = bi.createGraphics();
    biContext.drawImage(img, 0, 0, null);Greets
    PuceThis works fine; but has anybody else noticed this takes an unreasonable amount of time? How to get around this?

  • Converting an image object to  bufferedimage object

    Hi does anyone know how to convert an Image obj to a BufferedImage obj without casting...
    I have generated an image in an applet by rescaling a larger image but i cannot cast it to a BufferedImage as it was declared as an image type...
    thx

    Image img = setupImage(); // or however you set up your Image
    BufferedImage bimg = new BufferedImage(img.getWidth(), img.getHeight, BufferedImage.TYPE_ARGB);
    Graphics g = bimg.getGraphics();
    g.drawImage(img, 0, 0, null);

  • Need to convert an image to .jpeg using Java !

    Hello,
    i am working in images now. i need to convert any image in the form of .jpeg using Java. in other words, i need to store an image in the form of .jpeg format. can any of u help me ! thanks in advance.
    - Krishna

    There's also jimi, at http://java.sun.com/products/jimi/
    You can do something like:
    image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
    // create your image
    String path = "/path/image.jpg";
    Jimi.putImage(image, path);
    [\code]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Converting raw images into jpeg images

    I am trying to build a small application that can help me to convert raw images (of one of my cam) into JPG image.
    i have earched in google ans found many API ie, JRAWIO,JAI,mistral-0.9.6.809 and Imagero.. but i am not able to understand how to use them in my program..
    JRAWIO is helping me to convert NEF and TIFF. though it has functionality fto use it for CR2 ans othr but i failed to use them..
    plz help me out if possible with code(full if possible) and step by step description...

    Hi,
    we can not do the homework for you but we can help you out to the right direction.
    1- Get the input stream from the image file ( source )
    InputStream is = new BufferedInputStream(new FileInputStream(
                        "C:\\tmp\\sasha.jpg"));
    2- Get your BufferedImage
    BufferedImage buffImage = ImageIO.read(is);
    3- look at the API docs for BufferedImage it contains the image data ( Raster)
    4- I have not looked at the source code for the JRAWIO but I think it uses javax.imageio.* as an underlying transformation engine
    Regards,
    Alan Mehio
    London,UK

  • Convert colour images to grayscale images & get pixel data from them

    Is the code below correct to convert colour images to grayscale images in Java?
    public void convertToGrayscale (String sourceName,String destName) throws Exception {
    JPEGImageDecoder decoder=JPEGCodec.createJPEGDecoder(new FileInputStream(sourceName));
    JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(new FileOutputStream(destName));
    BufferedImage sourceImg=decoder.decodeAsBufferedImage();
    BufferedImageOp op =new ColorConvertOp(
              ColorSpace.getInstance(ColorSpace.CS_GRAY),null);
         BufferedImage destImg = op.filter(sourceImg,null);
    encoder.encode(destImg);
    decoder = null;
    encoder = null;
    When I get grayscale images from the code below, I would like to access the pixels of those images. So I tried to do:
    byte[] dd=((DataBufferByte)mImage.getRaster().getDataBuffer()).getData();
    BUT the data result array is not 0-255. Could anyone suggest how to obtain pixel data from grayscale images?
    In case that my code shown is not correct or suitable, please give your advice. What I would like to do are in the steps as follows:
    1 change 100*70 jpeg-images to 100*70 grayscale images.
    2 create two dimensional array of pixel data (example [100][70]) from converted images. The number in the array should be between 0-255, right??? And 0 refers to black colour and 255 refers to white colour???
    I am confused about grayscale images. Please help.
    Thank you so much

    I am not sure i understand what is the problem exactly.
    Structure of DataBuffer is described by SampleModel used by same Raster
    object. E.g. it might be 1 byte per xipex or 4 bytes per pixel.
    In your example convertToGrayscale saves images to file as JPEG and
    it seems you later read it back. It is possible what image you read back
    is not greyscale but ARGB and data buffer has different format.
    Technically, if you just need level of grey you may simply
    call getRGB on your output image. Grey is uniform mix of R, G and B.
    Also, instead of ColorConverOp you may dimply create output image of
    grayscale type and draw your color input image with drawImage().
    If none of these helps please try to provide more details.

  • Converting an image to a buffered image

    is there a faster way to convert an image into a buffered image. Ive looked around and the general solution is:
    BufferedImage bi = new BufferedImage(image.getWidth(), image.getHeight(),
    BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    g.drawImage(image, 0, 0, null);
    but that loads up 2 different varibles, g and bi, and this is going to be going in a loop. im under the impression 2 varibles is gonna slow it down, and the more fps i can get the better. I saw something about ImageIO, but from what i could gather that had to use url or stored file, rather than a Image type varible. whats the fastest whay to get a buffered image from an image. thx

    Presumably you are loading the image from a file or something.
    Why don't you load it directly into a buffered image using ImageIO? All you need to write is
    import javax.imageio.ImageIO;
    BufferedImage image = ImageIO.read(parameters...);
    there are several overloads, check the definitions here http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageIO.html

  • Java bufferedImage to JavaFX Image

    Hi,
    is there any easy way how to convert awt bufferedImage to JavaFX Image? I am using sun's pdf-renderer which output can be AWT image which I would like to display in a JavaFX scene as a image node.

    Yes. This has already been discussed here: Image conversion between AWT and FX

  • Converting a image file (JPG or BMP or any other) to an Image object

    Hello, does anyone have an idea of how I could convert a image file (JPG, BMP, GIF or any other) or even a Corel Draw (.CDR) file to a java Image object?
    Thanks in advance
    Wilson

    Demo:
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    import javax.swing.*;
    public class ImageResized {
        public static void main(String[] args) throws IOException {
            URL url = new URL("http://today.java.net/jag/bio/JagHeadshot.jpg");
            BufferedImage image = ImageIO.read(url);
            Icon icon = new ImageIcon(image);
            JLabel label = new JLabel(icon);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(label);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    }

  • How convert an image into its RGB value

    i want to find out how to convert an image files such as .gif or .jpg into its RGB value so that i can find out how much amount of a particular colors contain in the image.

    Try this:
    public int countColors(String filename) throws IOException {
    BufferedImage image = ImageIO.read(new FileInputStream(filename));
    Set colors = new HashSet();
    int height = image.getHeight();
    int width = image.getWidth();
    for (int x = 0; x < width; x++) {
    for (int y = 0; y < height; y++) {
    colors.add(new Integer(image.getRGB(x, y)));
    return colors.size();

  • Unable to convert an image (.bmp) to PDF

    I have a written servlet that uses Adobe LiveCycle API to convert files to PDF. This works. I have a problem with one image (.BMP) for which the PDF conversion fails. I then tried converting the image to PDF using Adobe Acrobat. I see the following error:
    I am able to open the image file without any issues. Why does Acrobat think the file is corrupt? Is there a way to find out if an image is OK for PDF conversion, either manually or programmatically? Attaching the image file for reference.

    Hi ,
    For this issue, it looks like your SharePoint server(s) have been patched with MS13-052. 
    Please try to uninstall it and reboot your machine.
    Here is a similar post for you to take a look at:
    https://social.technet.microsoft.com/Forums/sharepoint/en-US/eec6e515-6a0a-42f2-963d-51fde59be9c4/sharepoint-server-20102-abnormal-issues-please-help
    Best Regards,
    Wendy
    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact
    [email protected]

  • How can I convert an image created in Photoshop into a format that Java's ImageIO library will take?

    Hi all,
    I've been trying to write an image resize tool to use the Lancoz resample algorithm to resize images to a high standard in Java.  The library with the algorithm/filter in it is mortennobel's image scaling library and it takes a buffered image as an input and returns the resized image as desired.  However the trouble I’m having is that the ImageIO library in Java, a standard library, which converts images given to its read method to buffered images doesn't accept certain images created in Photoshop and so it throws an exception.  My library needs to be fully automated to process possible thousands of images at a time and so manually converting them by resaving them as JPEG's is really feasibly.  Is there some way I can convert these images from Adobes Exif format to standard JFIF format?  I've tried simple inserting the APP0 marker from a JFIF image in-between the SOI and APP1 marker but the ImageIO library still failed.
    The above image is a screenshot of the binary in hex of one of the images that cause an Invalid Format exception when passed to ImageIO.
    Any help you could offer me with this would be much appreciated.
    Kind regards and thank you,
    Alexei Blue
    Science Warehouse.
    NB: I previously posted this post in Developers but was told to post it in a Photoshop forum so apologies if this is the incorrect formum

    The image scaling library does have a BiCubicFilter in it so I'll experiment around with them and see what happens.  As for the imageIO library I think it accepts Exif image types, but still for some reason it just doesn't like Adobe Photoshop types.  Maybe its the colour map it doesn't like I just can't seem to work out what's wrong which is slightly frustrating but looking round to see if I can find an answer

  • How can i convert an image into gif file as jpeg by using com.sun.image.*

    please help me to convert an image into gif format. i have used sun's impl com.sun.image.code.jpeg package to convert a buffered image into a jpeg file. is there any implementatioln available from sun to handle gif files.

    Many. Try for instance google with 'java image encoders'. Go to the URL http://www.google.com/search?q=java+image+encoders and be amazed of the wonders of modern web search tools.

Maybe you are looking for

  • OS-DB Migration [ Import Error ]

    Hello, We are doing the OS-DB Migration and on the last stage of the import... We had 38 processes,out of which 37 are completed successfully... SAPAPPL0 is giving error... Kindly please suggest... trying to restart import ### (RIM) WARNING: unable t

  • Date Time in command prompt

    command is date <date> time <time> On 1 server, the format of date is <dd-mm-yy> On another server the date is <mm-dd-yy> Why is the date format different, how to standardise date format to <dd-mm-yy>?

  • XPath Error

    Hi folks, I am making a BPEL process in which I am assigning values from a temp variable to a variable named BEData. Then I am running a parallel flow each containing an adaptor service calling an API from database. Time & again I am encountering the

  • Oracle Access Manager API (ASDK) - ObUserSession / ObConfig Issues

    I am currently working with the ASDK and CoreID 7.0.4. I have gotten my custom Access Gate to the point where I wanted to start testing it out with my applications. I very quickly ran into some issues: Im my code I am making a connection to the Acces

  • Can't Start HTTP Server Service

    I can't seem to be able to start the Oracle HTTP server service on NT. All other service get started properly but when I attempt to start the HTTP Server service I get Error 1067: process terminated unexpectedly. Any idea why this is happening? My in