How convert Image in BufferedImage?

Hello i have a simple problem.
I have a Image and I would want to convert it in BufferedImage format.
Thanks

since I had the code anyway:
     public static BufferedImage getBufferedImage(Image img) {
          // if the image is already a BufferedImage, cast and return it
          if((img instanceof BufferedImage) && background == null) {
               return (BufferedImage)img;
          // otherwise, create a new BufferedImage and draw the original
          // image on it
          int w = img.getWidth(null);
          int h = img.getHeight(null);
          BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
          Graphics2D g2d = bi.createGraphics();
          g2d.drawImage(img, 0, 0, w, h, null);
          g2d.dispose();
          return bi;
     }

Similar Messages

  • Convert Image to BufferedImage

    how do i convert an Image object
    i) To a BufferedImage object
    2) To a RenderedImage object

    I tested the following code for copying a tif image onto clipboard and then pasting it on MSWord. It works fine, I have not tried the other way
    import java.awt.image.RenderedImage;
    import java.io.IOException;
    import javax.media.jai.RenderedImageAdapter;
    import util.TIFFFileHelper;
    public class ClipboardSupport implements ClipboardOwner{
         public ClipboardSupport() {
              super();
         public static void main(String[] args) throws IOException {
              ClipboardSupport co = new ClipboardSupport();
              RenderedImage img=co.getTestImage();
              co.copyToClipBoard(img);
         private RenderedImage getTestImage() throws IOException{
              TIFFFileHelper th = new TIFFFileHelper("C:/workspace/AV/src/images/onepageSample.tif");
              th.getPageCountParseFirstPage();
              RenderedImage img = th.getFirstPage();
              return img;
         public void copyToClipBoard(RenderedImage img2){
              Image img=this.getCovertedImage(img2);
              this.copyToClipBoard(img);
         public void copyToClipBoard(Image img){
              Toolkit tkt = Toolkit.getDefaultToolkit();
              Clipboard clipboard = tkt.getSystemClipboard();
              Transferable t = new ImageSelection(img);
              clipboard.setContents(t,this);
         public void lostOwnership(Clipboard clipboard, Transferable contents) {
              System.out.println("ClipboardSupport.lostOwnership() called");
         private Image getCovertedImage(RenderedImage img) {
              RenderedImageAdapter aid = new RenderedImageAdapter(img);
              Image im = aid.getAsBufferedImage();
              return im;
    package util.clipboard;
    import java.awt.Image;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.Transferable;
    import java.awt.datatransfer.UnsupportedFlavorException;
    import java.io.IOException;
    public class ImageSelection implements Transferable {
         private Image img=null;
         DataFlavor dataFlavor = DataFlavor.imageFlavor;
         public ImageSelection(Image img) {
              super();
              this.img=img;
         public DataFlavor[] getTransferDataFlavors() {          
              DataFlavor[] data = new DataFlavor[1];
              data[0]=dataFlavor;
              return data;
         public boolean isDataFlavorSupported(DataFlavor flavor) {          
              if(dataFlavor.equals(flavor)){
                   return true;
              }else{
                   return false;
         public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
              return img;
    }

  • 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!

  • How do I change my birthdate that was entered in on my Adobe App.  I want to convert images to pdf

    How do I change my birthdate that was entered in on my Adobe App.  I want to convert images to pdf

    What Adobe app?

  • How to convert image to binary format

    Hi all,
      We have developed an Employee search  mobile web application in .net which is hosted on an exposed IP server, we need to show the employee data along with the image of the employee on mobile.
    When we run this application through our desktop we are able to see the image of the employee since we are doing this through <b>intranet</b> , but when we try to access the same from any mobile device we are able to see only the data but no image, since we are doing this through <b>internet(exposed server).</b>
    Please suggest some way to get this image,
    is there any<b> function module in ABAP</b> which can <b>convert image to binary format</b>
    so that we <b>export binary data</b> to .net application

    Hei evryone!
    CAn anyone pls help me on how to solve this error:
    java.security.AccessControlException: access denied (java.security.SecurityPermission insertProvider.SunJCE)
         at java.security.AccessControlContext.checkPermission(Unknown Source)
         at java.security.AccessController.checkPermission(Unknown Source)
         at java.lang.SecurityManager.checkPermission(Unknown Source)
         at java.lang.SecurityManager.checkSecurityAccess(Unknown Source)
         at sun.plugin.security.ActivatorSecurityManager.checkSecurityAccess(Unknown Source)
         at java.security.Security.check(Unknown Source)
         at java.security.Security.insertProviderAt(Unknown Source)
         at java.security.Security.addProvider(Unknown Source)
         at CryptoTest.processFile(SwingApplet.java:68)
         at CryptoTest.<init>(SwingApplet.java:65)
         at SwingApplet.init(SwingApplet.java:39)
         at sun.applet.AppletPanel.run(Unknown Source)
         at java.lang.Thread.run(Unknown Source)
    Is it allright for a swing code to access local resources? like in my case i want my swing app to decrypt and encrypt an image file but when i tried to access the method for decrypting and encrypting i got this error message on my console. Do i have to make my code signed before i could write/read a file on my hard drive?
    Any help / suggestions would be much appreciated.Thanks!

  • How to convert image to array pixel values in java

    I would like to know how to convert image into arrary of pixels in java ?
    which API should i use ?
    please give me link to a sample program !

    I find the most efficient way to just walk over the image and call getRGB(x,y) on every pixel, more so than Pixelgrabber. If someone knows a more efficient way, give me a beep.

  • How do I convert images I enter into a PDF into text?

    Would you also happen to know how I convert images I enter into a PDF into text? (I can't use OCR if text already exists on that page of the document)

    Hey SinNombre,
    Could you please specify in detail what exactly do you wish for.
    Let me know what version of Acrobat are you using.
    Please tell me what exactly do you mean by saying convert 'images to text'. What happens when you try running OCR?
    Hope to hear from you.
    Regards,
    Anubha

  • How do I convert images to whatever icon MAc uses?

    Client: Mac OS 10.6.6
    Hi All,
    I am coming from the Linux side. In Linux, if I an find an Image I like, I can use it on my desktop Icons. Format is not an issue.
    Questions:
    1) in Mac, I have found it wants a particular format. How do I convert Images (jpeg, png, bitmap, tiff, etc.) to that format?
    2) once I have the image in place, do I need to keep the image around or is it copied into the icon's hidden (in Finder) application directory?
    Many thanks,
    -T

    For anybody using MacPorts, there is a port called makeicns that creates the needed .icns files from the command line.  Usage is pretty simple (from the command):
    makeicns v1.4.10 (284bd686824f)
    Usage: makeicns [k1=v1] [k2=v2] ...
    Keys and values include:
        512: Name of input image for 512x512 variant of icon
        256: Name of input image for 256x256 variant of icon
        128: Name of input image for 128x128 variant of icon
         32: Name of input image for 32x32 variant of icon
         16: Name of input image for 16x16 variant of icon
         in: Name of input image for all variants not having an explicit name
        out: Name of output file, defaults to first nonempty input name,
             but with icns extension
      align: [center, left, right, top, bottom] {First letter suffices!}
    Examples:
      makeicns -512 image.png -32 image.png
          Creates image.icns with only a 512x512 and a 32x32 variant.
      makeicns -in myfile.jpg -32 otherfile.png -out outfile.icns
          Creates outfile.icns with sizes 512, 256, 128, and 16 containing data
          from myfile.jpg and with size 32 containing data from otherfile.png.

  • How convert a big image to parts of images? means split a jpg image to several sub imges.(thi​s sub image can joint into that big image)

    hi friends..
    how convert a big image to parts of images? means split a jpg image to several sub imges.(this sub images can joint into that big image) any help
    Solved!
    Go to Solution.

    In the example, i have created two images and added them together, and the converse can be done in the same way.
    Thanks
    uday,
    Please Mark the solution as accepted if your problem is solved and help author by clicking on kudoes
    Certified LabVIEW Associate Developer (CLAD) Using LV13

  • How I may convert image to file?

    may previusly problem is join with   BufferedImage bimage = ImageIO.read(file); whose I not use previusly
    I may convert image file from filter to file '

    File imageFile = new File("myImage");
    try
        // img is your buffered image
        // screenshotType is "png", "jpg", "tif", "bmp"
        ImageIO.write(img, screenshotType, imageFile);
    catch(IOException e)
        System.out.println("IO Error while trying to save image file");
    }Edited by: Nighttime on Feb 9, 2008 5:50 PM

  • How can i convert image into pdf  file

    Hi Friends,
    I want to convert image file(i.e. jpeg,bmp,gif) into pdf file using java apis OR is there any tool which can convert it & which we can integrate into our java application
    Thanks in adva.

    Is there any solution to this case.....?

  • Converting images using ImageIO

    hello all,
    I am in need of help asap.
    Does anyone know how to convert images from jpg/gif to PNG format using Imageiio? I will be very grateful if u can pass this information on to me. I need to convert the images on a server side and send the png image to a mobile (j2me) client.
    Please respond to this if you have ideas on this.
    cheers
    cp

    quite simple.
    for example, you got abc.gif , want to convert to jpp
    BufferedImage bi = ImageIO.read(new File("abc.gif"));
    ImageIO.write(bi, "jpg", new File("abc.jpg"));

  • How many images per sec can I get from ImageIO.read(url) ??????

    Hello,
    In my program I read images from a url...I'm wondering how many images I can get with ImageIO.read(url) per second..
    Hereby is the code that I'm using:
    import java.awt.*; //Contains all of the classes for creating user interfaces and for painting graphics and images
    import java.awt.event.*;//Provides interfaces and classes for dealing with different types of events fired by AWT components
    import java.awt.image.*;//Provides classes for creating and modifying images
    import java.io.*;//Provides for system input and output through data streams, serialization and the file system
    import java.net.URL;
    import javax.imageio.*;//The main package of the Java Image I/O API.
    import javax.swing.*;//Provides a set of "lightweight" (all-Java language) components that, to the maximum degree possible, work the same on all platforms.
    import java.text.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    public class getPic extends Component{
    private BufferedImage img;
        static int n=0;
        private URL url;
        private DateFormat dateFormat;
        private Date date;
        private String s;
        private String str1= ".jpeg";
        private String str2="C:\\Users\\";
        private String str3;
        private String str4;
          public getPic() {
         try {
                  url = new URL("http://"); //a url that gives a real-time image
                  img = ImageIO.read(url);
                } catch (IOException e) {
               System.err.println("Unable to read file");
    public void savePic(){
    try{
    n++;
    str3=str2.concat(Integer.toString(n-1));
                        str4=str3.concat(str1);
                        ImageIO.write(img, "jpeg" , new File(str4));
                    } catch(IOException e) {
                      System.err.println("Unable to output results");
    @Override
        public Dimension getPreferredSize() {
            if (img == null) {
               return new Dimension(100,100);
            } else {
               return new Dimension(img.getWidth(), img.getHeight());
        @Override
          public void paint(Graphics g) {  //http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Paint.html
            g.drawImage(img, 10, 10, null);//http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html
        public static void main(String[] args) throws IOException {
           JFrame f = new JFrame(" Image without processing!!");
           f.addWindowListener(new WindowAdapter(){//http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/WindowListener.html
                @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
        int i=0;
        for( ; ; ){
            i++;
            getPic pi = new getPic();
            pi.savePic();
            f.add(pi);
            f.pack();  //Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.
            f.setVisible(true);
         try
            Thread.sleep(1000);
            }catch (InterruptedException ie)
            System.out.println(ie.getMessage());
    }Thank you in advance for your answers
    Joan

    Finally I solved my problem(getting as many images as possible from a url infinitely) using the above code:
    import java.net.*;
    import java.io.*;
    public class UserApplication {
        private static int n=0;
        String url;
      public void UserApplication(){
        public static void main(String[] args) throws Exception {
            UserApplication app= new UserApplication();
            for(;;){
            app.urlStr();
        private void urlStr(){
            try{
                url= "http://mplamplampla/frame.php/";
                HttpURLConnection con=(HttpURLConnection) ((new URL(url).openConnection()));
                BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream("C:\\Users\\mpla\\Desktop\\" + n + ".jpeg"));
                con.setDoInput(true);
                con.setDoOutput(false);
                con.setRequestMethod("GET");
                BufferedInputStream in = new BufferedInputStream(con.getInputStream());
                int bt = 0;
                byte[] buffer = new byte[4096];
                while ((bt = in.read(buffer, 0, 4096)) > -1) {
                  out.write(buffer, 0, bt);
                in.close();
                out.close();
                System.out.println("Image " + n + " saved");
                n++;
                } catch (Exception e) {e.printStackTrace();}
    }

  • How to Pass a BufferedImage? Client - Server

    Please Help!
    I've been trying to figure out how to send a BufferedImage from my client program to the server program. I'm using a socket to create the connection. The client draws into a BufferedImage and then it is sent to the server to be saved as a JPG file. I've been able to create the image if I do it locally but since a BufferedImage is not serializable I can't do this the same way with the server. So how do I get my BufferedImage from the client to the server in a manner that I can then save it as a JPG on the server? Code examples would be very much appreciated!!!
    Thanks!
    Ryan

    I guess I'm not understanding what your saying. I just need a way to get what the user draws into a jpg on the server. I have been using sockets and streams but nothing I try really seems to work. Right now I am doing this:
    Client:
    Socket client = new Socket( InetAddress.getByName( "localhost" ), 5000 );
    ObjectOutputStream output = new ObjectOutputStream( client.getOutputStream() );
    ObjectInputStream input = new ObjectInputStream( client.getInputStream() );
    try {
    ByteArrayOutputStream imageOut = new ByteArrayOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( imageOut );
    imageOut.flush();
    encoder.encode( sig.getImage() );
    byte[] image = imageOut.toByteArray();
    output.writeObject( image );
    Server:
    public void runServer() {
    ServerSocket server;
    Socket connection;
    try
    server = new ServerSocket( 5000, 100 );
    while ( true ) {
    connection = server.accept();
    output = new ObjectOutputStream(
    connection.getOutputStream() );
    input = new ObjectInputStream(
    connection.getInputStream() );
    ObjectOutputStream toFile = new ObjectOutputStream(
    new FileOutputStream( f ) );
    ByteArrayInputStream inStream =
    new ByteArrayInputStream( (byte[]) input.readObject() );
    JPEGImageDecoder decoder =
    JPEGCodec.createJPEGDecoder( inStream );
    BufferedImage sigImage = decoder.decodeAsBufferedImage();
    JPEGImageEncoder encoder =
    JPEGCodec.createJPEGEncoder( toFile );
    encoder.encode( sigImage );
    toFile.flush();
    output.close();
    input.close();
    connection.close();
    toFile.close();
    I imagine I have very ugly looking code here but I've been trying to make something work. My code does create the file but it is not a recognizable JPG file.
    Ryan

  • CONVERTING IMAGES

    How do I convert an image pdf file to jpg format?

    Hi Doggydaddyo,
    To convert image PDF file to .jpeg file, you need to use the complete PDF solution Adobe Acrobat.
    For more information kindly visit : http://www.adobe.com/products/acrobat.html?promoid=JOLIR
    let me know if you ahve any other question.
    Regards,
    ~Pranav

Maybe you are looking for