Results 1 to 11 of 11
  1. #1
    Aryaa is offline Member
    Join Date
    Jun 2015
    Posts
    42
    Rep Power
    0

    Default how to save image as jpg?

    Java Code:
    [COLOR="#0000FF"]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;
        }
    }
    [COLOR="#FF0000"]This error comes While compiling #2 Codes...[/COLOR]
     by: java.lang.ClassNotFoundException: Jpg
      at java.net.URLClassLoader$1.run(URLClassLoader.java:2
      at java.security.AccessController.doPrivileged(Native
      at java.net.URLClassLoader.findClass(URLClassLoader.ja
      at java.lang.ClassLoader.loadClass(ClassLoader.java:30
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher
      at java.lang.ClassLoader.loadClass(ClassLoader.java:24
    not find the main class: Jpg.  Program will exit.
    
    gram Files\Java>[/COLOR]
    Last edited by Aryaa; 06-24-2015 at 04:42 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: how to save image as jpg?

    Post moved to its own thread. Please don't hijack old threads. Start your own. They are free.

    Please edit the post and wrap the code in code tags to preserve its formatting.

    The JVM can not find a definition for the Jpg class. Where is its definition?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: how to save image as jpg?

    I don't get it, you say you get errors when you compile, yet you post an error you would get when running the application.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: how to save image as jpg?

    "not find the main class: Jpg. Program will exit."

    That implies you are trying to run the class Jpg, but the name of the class you have shown us is SavingAnImage .
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: how to save image as jpg?

    Folks, this is nothing more than pasted code from a very old thread. It isn't even the this OP's thread. I presume this individual was trying to "help" the older threads OP.

    http://www.java-forums.org/new-java/...image-jpg.html

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: how to save image as jpg?

    The original post is like 8 years old, no wonder you're getting errors.
    How can you even expect it to work after the billion updates Java did since.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: how to save image as jpg?

    Quote Originally Posted by Artemia View Post
    The original post is like 8 years old, no wonder you're getting errors.
    How can you even expect it to work after the billion updates Java did since.
    Ignoring the hyperbole, working source code from the earlier releases of Java should still work except in rare circumstances (like a bug which someone took advantage of but was fixed in a later release). Even new JRE's should run code compiled from older versions of Java. Otherwise, every time someone wanted to upgrade, they would have to recompile all of their sources. It's referred to as backward compatibility.

    Regards,
    Jim
    Last edited by jim829; 06-24-2015 at 05:25 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: how to save image as jpg?

    really? cause when I was working from a 2 year old book on Java eclipse kept telling me that the classes I was told told to use/import were outdated so I wasn't allowed to use them :(
    it was very frustrating.
    I figured AWT stuffs would be like that too

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: how to save image as jpg?

    Quote Originally Posted by Artemia View Post
    really? cause when I was working from a 2 year old book on Java eclipse kept telling me that the classes I was told told to use/import were outdated so I wasn't allowed to use them :(
    it was very frustrating.
    I figured AWT stuffs would be like that too
    I presume that you meant deprecated. You are permitted to use them. However, you are discouraged from using them because there are better choices in newer versions. And if I chose to do so I could write an AWT based program using Java 8. But with Swing and more recently, JavaFX, programmers are encouraged not to do so. It is also possible that under the errors and warnings preferences, Eclipse was configured to flag deprecated classes as an error. I don't remember much of AWT being deprecated though so I am surprised you got many errors.

    Regards,
    Jim
    Last edited by jim829; 06-24-2015 at 05:41 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: how to save image as jpg?

    Oh I see.
    Thanks for the info :)

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: how to save image as jpg?

    Quote Originally Posted by jim829 View Post
    Folks, this is nothing more than pasted code from a very old thread. It isn't even the this OP's thread. I presume this individual was trying to "help" the older threads OP.

    http://www.java-forums.org/new-java/...image-jpg.html

    Regards,
    Jim
    Thanks for noticing; that behaviour of the OP is just silly; he caused enough havoc and I banned him for two weeks.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 1
    Last Post: 07-28-2011, 08:48 AM
  2. How to save image in database
    By sandeep23k in forum Advanced Java
    Replies: 3
    Last Post: 08-02-2010, 12:02 PM
  3. how to save image as jpg?
    By katie in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 10:07 AM
  4. Save image as JPEG
    By aartheesrini in forum Java Applets
    Replies: 1
    Last Post: 03-16-2009, 05:56 AM
  5. how can i save an image from url
    By happyday in forum Advanced Java
    Replies: 2
    Last Post: 01-14-2009, 07:02 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
  •