A personal repository of random information in compensation for a fatigued biological computer
Breaded IT » Web Development » SilverStripe CMS pre V3.0 » Silverstripe 2.2.x Notes » JS to allow image paste in TinyMCE
http://tinymce.moxiecode.com/punbb/viewtopic.php?pid=18088#p18088
Code:public class PasteImageApplet extends JApplet{ Clipboard clipboard; Toolkit toolkit; public String getClipboardImageURL(String server){ 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+"/uploader?"); URLConnection con = u.openConnection(); String boundary = "-----------------------------7d637a1aa100de"; con.setRequestProperty("method", "POST"); con.setRequestProperty("Content-Type", "multipart/form-data; boundary=\""+boundary+"\""); con.setDoOutput(true); 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.encode(stream.toByteArray())).getBytes()); con.connect(); InputStream inputStream = con.getInputStream(); byte [] urlBytes = new byte [inputStream.available()]; inputStream.read(urlBytes); url = new String(urlBytes); url = "/getImage?id="+url; System.out.println(url); } catch (Exception exc){ if (ShowExceptions.ShowExceptions) exc.printStackTrace(); } return url; } public void init() { add(new JLabel("applet started")); toolkit = Toolkit.getDefaultToolkit(); clipboard = toolkit.getSystemClipboard(); } |