Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default How to Convert .jpg to .bmp Image Format

    Hi All,
    I Need to convert .jpg file to a .bmp file, When i am trying to convert it the converted image could not opening.

    //Create file for the source
    File input = new File("c:/temp/image.jpg");

    //Read the file to a BufferedImage
    BufferedImage image = ImageIO.read(input)



    //Create a file for the output
    File output = new File("c:/temp/image.bmp");

    //Write the image to the destination as a JPG
    ImageIO.write(image, "bmp", output);




    I am Trying Above code to try this can any one suggest how to do this without changing the quality.


    Waiting for Positive Comments which will help me Out.

    Thanks in Advance....

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    To get better help sooner, post a SSCCE that clearly demonstrates your problem.

    And post your code in code tags, not that silly bold font.

    db

  3. #3
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default

    Thanks Mr.Darryl.Burke,

    Sorry For that Bold Font. Here is the code which i am using

    Java Code:
    import java.io.*;
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    public class ConvertJPGToBMP
    {
    public static void main(String a[]){
      try{  
      System.out.println("Enter image name\n");
      BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
      String imageName=bf.readLine();
      File input = new File(imageName);
      BufferedImage image = ImageIO.read(input);
      System.out.println("Enter the output image name(.bmp):\n");
      String imageName1=bf.readLine();  
      File output = new File(imageName1);
      ImageIO.write(image, "bmp", output);
      System.out.println("Your image has been converted successfully");
    }catch(FileNotFoundException e){
      System.out.println("Error:"+e.getMessage());
    }catch(IOException e)
      {
      System.out.println("Error:"+e.getMessage());
      }
      catch(Exception e){
      System.out.println(e.getMessage());
      }
    
    }
    }

  4. #4
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    First of all what is the output of this? Doesn't compile? Some error? Doesn't do anything? Help us a bit. Second you could check How to Use File Choosers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) instead of entering a name of a file which you could spell wrong or whatever.

  5. #5
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Think I found your error. On the "System.out.println("Enter the output image name(.bmp):\n");" if you write the whole destination path (e.g. C:\new.bmp) it works. So find a way to get the source path and pass it to the destination ;)

  6. #6
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default

    Thanks santeron,

    I Need to convert the Files which are on specific location into my system.then all files have to me same name as previous just need to convert to bmp.

    eg. d:\Newfolder\ 1.jpg, 2.jpg,3.jpg

    output : d:\output\1.bmp,2.bmp,3.bmp


    How can i achive this.

    thanks in advance

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default

    What's holding you back from answering santeron's question?
    First of all what is the output of this? Doesn't compile? Some error? Doesn't do anything?
    db

  8. #8
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    If you want to convert multiple files you could either do a while loop for the user to enter one by one the files or you could set a source path for the app to search by it's own, get files and convert those ending in .jpg which is a little harder. Let's say you go for the first option and do a while loop. You could have some code looking like this:
    Java Code:
    Enter source folder
    Enter destination folder
    while (true) {
       enter file name (which should be in source folder)
       if file exists {
           convert file to bmp and put it in output folder
           show message of success
       } else {
           show error message
           ask if he wants to convert another file (y/n)
           if (y) {
                 continue;
             } else {
                 system.exit(0);
             }
        ask if he wants to convert another file (y/n)
        if (n) {
            system.exit(0); (or brake;)
        }
    } //end loop
    You could try doing it with fileChoosers both for opening and saving for easy folder change :)

  9. #9
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default

    Hello Darryl,

    Sorry for same, But as he mentioned that there could be only one problem of entering the file name with complete path can be.


    Can You suggest me How i can convert an array of images(.jpg) files to (.bmp) files.

    Please come with Suggestion.

    Thanks.

  10. #10
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default

    Thanks Mr.santeron,

    This will help me a Lot, Can you tell me brief about the second way u suggested to me.

  11. #11
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    The use could write a series of file names delimited by a certain character like ";" or whatever. So lets say he wrote "image1.jpg image2.jpg" (i used space as the delimiter) you take that String and use the split method of Strings.

    String input = "image1.jpg image2.jpg";
    String[] imageArray = input.split(" ");

    now you have an array of strings that you can loop through.

  12. #12
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Check a couple of links I found on populating files in folder. Search/Read don't except us to tell you what to do. We could suggest or lead you to a conclusion ;)

    Listing the Files or Subdirectories in a Directory | Example Depot
    open and read all files from folder - Java

  13. #13
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

Similar Threads

  1. convert .txt file in .csv format
    By rajuchacha007 in forum New To Java
    Replies: 19
    Last Post: 03-18-2010, 09:10 AM
  2. How Convert a Byte array to a image format
    By perlWhite in forum New To Java
    Replies: 2
    Last Post: 08-23-2009, 08:11 PM
  3. How Convert a Byte array to a image format
    By perlWhite in forum Advanced Java
    Replies: 1
    Last Post: 08-22-2009, 07:05 PM
  4. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 10:21 AM
  5. how to convert date format
    By saran123 in forum New To Java
    Replies: 5
    Last Post: 10-16-2008, 06:10 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
  •