Results 1 to 6 of 6
  1. #1
    Ninstra is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default Image from clipboard bad quality

    Hello, I am PHP programmer and not so good at java so any help about how to increase the image quality is appriciated.

    I have found a code that takes image from the users clipboard(after they press print screen button) and sends it to the server, but the quality is not the same as after taking a print screen and then pasted into ex. paint.

    Here is the java code i have:
    PHP Code:
    import java.awt.datatransfer.Clipboard;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.Toolkit;
    import java.awt.Color;
    import java.awt.image.BufferedImage;
    import java.awt.image.ImageObserver;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;
    import javax.swing.ImageIcon;
    import java.io.ByteArrayOutputStream;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
    import com.sun.image.codec.jpeg.JPEGCodec;
    import java.net.URL;
    import java.net.URLConnection;
    import java.io.InputStream;
    import javax.swing.JLabel;
    
    public class PasteImageApplet extends JApplet{
    
        Clipboard clipboard;
        Toolkit toolkit;
    	JLabel lbl;
    
        public String getClipboardImageURL(String server){
    		lbl.setText("pasting image");
    
            String url = "";
            try{
                DataFlavor dataFlavor = DataFlavor.imageFlavor;
                System.out.println(dataFlavor.getDefaultRepresentationClass());
                Object object  = null;
    
                try{
                    object = clipboard.getContents(null).getTransferData(dataFlavor);
                }catch (Exception e){
                    JOptionPane.showMessageDialog(null, "No image found.");
                    return "";
                }
    
                BufferedImage img = (BufferedImage) object;
    
                BufferedImage bimg = null;
                int w = img.getWidth(null);
                int h = img.getHeight(null);
                bimg = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
    
    
                ImageIcon ii = new ImageIcon(img);
                ImageObserver is = ii.getImageObserver();
    
                bimg.getGraphics().setColor(new Color(255, 255, 255));
                bimg.getGraphics().fillRect(0, 0, w, h);
                bimg.getGraphics().drawImage(ii.getImage(), 0, 0, is);
    
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(stream);
                jpeg.encode(bimg);                                               
    
                URL u = new URL(server);
                URLConnection con = u.openConnection();
                //String boundary = "-----------------------------7d637a1aa100de";
                con.setDoOutput(true);
                con.getOutputStream().write(stream.toByteArray());
                /*con.getOutputStream().write(((String)
                        "--"+boundary+"\r\n "+
                        "Content-Disposition: form-data; name=\"img\"; filename=\"filename\"\r\n"+
                        "Content-Type: image/jpeg\r\n "+
                        "Content-Transfer-Encoding: base64\r\n\r\n" +
                        Base64.encodeBytes(stream.toByteArray())).getBytes());*/
                con.connect();
                InputStream inputStream = con.getInputStream();
                byte [] urlBytes = new byte [inputStream.available()];
                inputStream.read(urlBytes);
                url = new String(urlBytes);
                System.out.print(url);
    	        lbl.setText("image pasted");
            } catch (Exception exc){
    	        lbl.setText("an error occurred: " + exc.getMessage());
                /*if (ShowExceptions.ShowExceptions)
                    exc.printStackTrace();*/
            }
            return url;
        }
    
        public void init() {
    		lbl = new JLabel("");
    		lbl.setText("applet started");
    		add(lbl);
            toolkit = Toolkit.getDefaultToolkit();
            clipboard = toolkit.getSystemClipboard();
        }
    }
    Last edited by Ninstra; 09-08-2010 at 03:53 PM. Reason: changed javacode

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,584
    Rep Power
    23

    Default

    Where is the class PNGImageEncoder defined? I get a 'cannot find symbol' compile error.

  3. #3
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Default

    I guess you shouldn't encode image as

    Java Code:
    PNGImageEncoder jpeg = PNGCodec.createPNGEncoder(stream); 
    jpeg.encode(bimg);
    before sending...

    Because, as I remember, PNG decreases high resolution image quality :confused:

  4. #4
    Ninstra is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi again. Sorry the javacode in my first post was not the original one I found(i have edited that post now). I tryed myself to change from JPG imagetype to PNG because i believed it would make a difference. But I maby did something wrong.

    So is it possible the JPEGImageEncoder has a default quality value? And needs to be set to 100%?

    I also need help making it send as PNG instead of JPG.

    Thanks.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,584
    Rep Power
    23

    Default

    Wasting everyone's time by posting the wrong code.
    Let us know when you've posted the right code.

  6. #6
    Ninstra is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Yes i know Im sorry. The code that is in the first post now is working. It is sending back JPG image.

Similar Threads

  1. Offering Quality Routes & Call Termination
    By taiger786 in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-17-2010, 09:00 AM
  2. High Quality .....
    By farzana shamim in forum Forum Lobby
    Replies: 1
    Last Post: 07-22-2009, 02:43 PM
  3. Image output quality
    By linus_k in forum New To Java
    Replies: 8
    Last Post: 03-16-2009, 05:49 PM
  4. How to draw 2 images with quality?
    By Panchitopro in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 08:31 AM
  5. Sending Image Objects through the Clipboard
    By Java Tip in forum java.awt.datatransfer
    Replies: 0
    Last Post: 04-16-2008, 10:52 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
  •