public static String extractText(String st) { BufferedImage BI = null; File f = new File(st); try { BI = ImageIO.read(f); } catch (IOException ex) { System.out.println("Image Not readable or Available ::" + ex); } int width = BI.getWidth(); int height = BI.getHeight(); // BufferedImage BI=new BufferedImage(f); int TotalChars = ImageUtilities.getBlue(BI, width - 1, height - 1); char Chars[] = new char[TotalChars]; int ind = 0; for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++) { if (ind < TotalChars) { if (ind % 3 == 0) Chars[ind] = (char) ImageUtilities.getRed(BI, w, h); else if (ind % 3 == 1) Chars[ind] = (char) ImageUtilities.getGreen(BI, w, h); else if (ind % 3 == 2) Chars[ind] = (char) ImageUtilities.getBlue(BI, w, h); ind++; } } } return (new String(Chars)); }
public static void store( String codecName, java.io.OutputStream outputStream, java.awt.Image image, edu.cmu.cs.stage3.image.codec.ImageEncodeParam imageEncodeParam) throws InterruptedException, java.io.IOException { int width = ImageUtilities.getWidth(image); int height = ImageUtilities.getHeight(image); java.awt.image.RenderedImage renderedImage; if (codecName.equals("jpeg")) { java.awt.Image originalImage = image; image = new java.awt.image.BufferedImage( width, height, java.awt.image.BufferedImage.TYPE_3BYTE_BGR); java.awt.Graphics g = image.getGraphics(); g.drawImage( originalImage, 0, 0, new java.awt.image.ImageObserver() { @Override public boolean imageUpdate( java.awt.Image image, int infoflags, int x, int y, int width, int height) { return true; } }); // todo: does dispose ensure the image is finished drawing? g.dispose(); } if (image instanceof java.awt.image.RenderedImage) { renderedImage = (java.awt.image.RenderedImage) image; } else { int[] pixels = ImageUtilities.getPixels(image, width, height); java.awt.image.BufferedImage bufferedImage = new java.awt.image.BufferedImage( width, height, java.awt.image.BufferedImage.TYPE_INT_ARGB); bufferedImage.setRGB(0, 0, width, height, pixels, 0, width); renderedImage = bufferedImage; } if (imageEncodeParam == null) { if (codecName.equals("png")) { imageEncodeParam = edu.cmu.cs.stage3.image.codec.PNGEncodeParam.getDefaultEncodeParam(renderedImage); } } java.io.BufferedOutputStream bufferedOutputStream; if (outputStream instanceof java.io.BufferedOutputStream) { bufferedOutputStream = (java.io.BufferedOutputStream) outputStream; } else { bufferedOutputStream = new java.io.BufferedOutputStream(outputStream); } edu.cmu.cs.stage3.image.codec.ImageEncoder imageEncoder = edu.cmu.cs.stage3.image.codec.ImageCodec.createImageEncoder( codecName, bufferedOutputStream, imageEncodeParam); imageEncoder.encode(renderedImage); bufferedOutputStream.flush(); }
/** * Opens a file and displays the image. This method is used by main and open item. * * @param filename the file to be opened */ private static void openImageFile(String filename) { // Delegate the problem of reading the filename to another method! BufferedImage img = ImageUtilities.loadImage(filename); if (img == null) { JOptionPane.showMessageDialog(null, filename + " could not be opened."); } else openImage(img, filename); // create the window with a title - see // below }
@Override // to do on main thread again protected void onPostExecute(byte[] result) { if (result == null) { Log.i(TAG, "ImageDownloader; image is null"); imageView.setImageResource(R.drawable.garden_lits_item_picture); return; } // process message // convert back to Image Bitmap image = ImageUtilities.getImage(result); // print out on ImageView imageView.setImageBitmap(image); }
public static boolean sendData(Context c, Notification n, String packageName) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); ConnectivityManager connManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); // Check Wifi state, whether notifications are enabled globally, and // whether notifications are enabled for specific application if (prefs.getBoolean("pref_toggle", true) && prefs.getBoolean(packageName, true) && mWifi.isConnected()) { String ip = prefs.getString("pref_ip", "0.0.0.0:9090"); // Magically extract text from notification ArrayList<String> notificationData = NotificationUtilities.getNotificationText(n); // Use PackageManager to get application name and icon final PackageManager pm = c.getPackageManager(); ApplicationInfo ai; try { ai = pm.getApplicationInfo(packageName, 0); } catch (final NameNotFoundException e) { ai = null; } String notificationBody = ""; String notificationHeader = ""; // Create header and body of notification if (notificationData.size() > 0) { notificationHeader = notificationData.get(0); if (notificationData.size() > 1) { notificationBody = notificationData.get(1); } } else { return false; } for (int i = 2; i < notificationData.size(); i++) { notificationBody += "\n" + notificationData.get(i); } // Append application name to body if (pm.getApplicationLabel(ai) != null) { if (notificationBody.isEmpty()) { notificationBody = "via " + pm.getApplicationLabel(ai); } else { notificationBody += " (via " + pm.getApplicationLabel(ai) + ")"; } } // Setup HTTP request MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); // If the notification contains an icon, use it if (n.largeIcon != null) { entity.addPart( "notificon", new InputStreamBody(ImageUtilities.bitmapToInputStream(n.largeIcon), "drawable.png")); } // Otherwise, use the application's icon else { entity.addPart( "notificon", new InputStreamBody( ImageUtilities.bitmapToInputStream( ImageUtilities.drawableToBitmap(pm.getApplicationIcon(ai))), "drawable.png")); } HttpPost post = new HttpPost("http://" + ip + "/notif"); post.setEntity(entity); try { post.addHeader( "notifheader", Base64.encodeToString( notificationHeader.getBytes("UTF-8"), Base64.URL_SAFE | Base64.NO_WRAP)); post.addHeader( "notifdescription", Base64.encodeToString( notificationBody.getBytes("UTF-8"), Base64.URL_SAFE | Base64.NO_WRAP)); } catch (UnsupportedEncodingException e) { post.addHeader( "notifheader", Base64.encodeToString(notificationHeader.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP)); post.addHeader( "notifdescription", Base64.encodeToString(notificationBody.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP)); } // Send HTTP request HttpClient client = new DefaultHttpClient(); HttpResponse response; try { response = client.execute(post); String html = EntityUtils.toString(response.getEntity()); if (html.contains("true")) { return true; } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return false; }