Created
August 20, 2012 17:49
-
-
Save timpulver/3406114 to your computer and use it in GitHub Desktop.
Upload an image to image hoster Imgur (can be used for PGraphics, too)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.Graphics2D; | |
| import java.awt.image.BufferedImage; | |
| import java.io.BufferedReader; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.io.OutputStreamWriter; | |
| import java.io.UnsupportedEncodingException; | |
| import java.net.HttpURLConnection; | |
| import java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.net.URLConnection; | |
| import java.net.URLEncoder; | |
| import javax.imageio.ImageIO; | |
| import processing.core.PApplet; | |
| import processing.core.PGraphics; | |
| //import processing.video.*; | |
| import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; | |
| public class ImgurImageUploadTest extends PApplet{ | |
| private final static String API_KEY = "INSERT_YOUR_KEY_HERE"; | |
| //Capture myCapture; | |
| /** | |
| * @param args | |
| */ | |
| public void setup() { | |
| size(600, 600); | |
| uploadImage(); | |
| } | |
| public void uploadImage(){ | |
| // Creates Byte Array from picture | |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
| BufferedImage imageToWrite = getImage(); | |
| try { | |
| ImageIO.write(imageToWrite, "png", baos); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| URL url = null; | |
| try { | |
| url = new URL("http://api.imgur.com/2/upload"); | |
| } catch (MalformedURLException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| //encodes picture with Base64 and inserts api key | |
| String data = ""; | |
| try { | |
| data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encode(baos.toByteArray()), "UTF-8"); | |
| data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(API_KEY, "UTF-8"); | |
| } catch (UnsupportedEncodingException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| // opens connection and sends data | |
| URLConnection conn = null; | |
| try { | |
| conn = url.openConnection(); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| conn.setDoOutput(true); | |
| OutputStreamWriter wr = null; | |
| try { | |
| wr = new OutputStreamWriter(conn.getOutputStream()); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| try { | |
| wr.write(data); | |
| wr.flush(); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| // Get the response | |
| InputStream is = null; | |
| try { | |
| if (((HttpURLConnection) conn).getResponseCode() == 400) | |
| is = ((HttpURLConnection) conn).getErrorStream(); | |
| else | |
| try { | |
| is = conn.getInputStream(); | |
| } catch (IOException e1) { | |
| // TODO Auto-generated catch block | |
| e1.printStackTrace(); | |
| } | |
| } catch (IOException e1) { | |
| // TODO Auto-generated catch block | |
| e1.printStackTrace(); | |
| } | |
| BufferedReader rd = new BufferedReader(new InputStreamReader(is)); | |
| String line; | |
| try { | |
| while ((line = rd.readLine()) != null) { | |
| System.out.println(line); | |
| } | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| try { | |
| rd.close(); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| BufferedImage getImage(){ | |
| PGraphics pg; | |
| pg = createGraphics(width, height,JAVA2D); | |
| pg.beginDraw(); | |
| pg.background(0); | |
| pg.fill(255); | |
| pg.ellipse(50, 50, 50, 50); | |
| pg.endDraw(); | |
| BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE ); | |
| Graphics2D g2d = img.createGraphics(); | |
| g2d.drawImage((java.awt.Image)pg.image, 0, 0, width, height, this); | |
| g2d.finalize(); | |
| g2d.dispose(); | |
| return img; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment