Results 1 to 6 of 6
Thread: Image from clipboard bad quality
- 09-07-2010, 04:17 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
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
- 09-07-2010, 05:10 PM #2
Where is the class PNGImageEncoder defined? I get a 'cannot find symbol' compile error.
- 09-08-2010, 12:39 AM #3
I guess you shouldn't encode image as
before sending...Java Code:PNGImageEncoder jpeg = PNGCodec.createPNGEncoder(stream); jpeg.encode(bimg);
Because, as I remember, PNG decreases high resolution image quality :confused:
- 09-08-2010, 03:57 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
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.
- 09-08-2010, 04:04 PM #5
Wasting everyone's time by posting the wrong code.
Let us know when you've posted the right code.
- 09-08-2010, 04:06 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Offering Quality Routes & Call Termination
By taiger786 in forum Reviews / AdvertisingReplies: 0Last Post: 02-17-2010, 09:00 AM -
High Quality .....
By farzana shamim in forum Forum LobbyReplies: 1Last Post: 07-22-2009, 02:43 PM -
Image output quality
By linus_k in forum New To JavaReplies: 8Last Post: 03-16-2009, 05:49 PM -
How to draw 2 images with quality?
By Panchitopro in forum Java 2DReplies: 1Last Post: 03-12-2009, 08:31 AM -
Sending Image Objects through the Clipboard
By Java Tip in forum java.awt.datatransferReplies: 0Last Post: 04-16-2008, 10:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks