List of usage examples for java.io ByteArrayOutputStream flush
public void flush() throws IOException
From source file:Main.java
public static byte[] getCompressedImage(byte[] rgb) { BufferedImage image;//from w w w. ja v a 2 s . com int width; int height; try { image = ImageIO.read(new ByteArrayInputStream(rgb)); width = image.getWidth(); height = image.getHeight(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Color c = new Color(image.getRGB(j, i)); int red = (int) (c.getRed() * 0.299); int green = (int) (c.getGreen() * 0.587); int blue = (int) (c.getBlue() * 0.114); Color newColor = new Color(red + green + blue, red + green + blue, red + green + blue); image.setRGB(j, i, newColor.getRGB()); } } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", baos); baos.flush(); byte[] imageInByte = baos.toByteArray(); baos.close(); return imageInByte; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String imageDigest(BufferedImage img) { // write image to byte stream ByteArrayOutputStream os = new ByteArrayOutputStream(); try {/*from ww w . j av a 2 s. co m*/ ImageIO.write(img, "png", os); os.flush(); } catch (IOException e) { e.printStackTrace(); return null; } byte[] data = os.toByteArray(); // compute md5 hash byte[] hash = null; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(data); hash = md.digest(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); return null; } // convert to string String hexString = ""; for (int i = 0; i < hash.length; i++) { hexString += Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1); } return hexString; }
From source file:Main.java
public static byte[] compressBitmap(Bitmap bitmap) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); if (bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteStream)) { try {// w w w. ja v a 2 s . c o m byteStream.flush(); byteStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } return byteStream.toByteArray(); }
From source file:Main.java
/** * Converts image to byte array.//from w ww . j a v a 2 s. c o m * <br /><br /> * <p/> * <b>ATTENTION</b>: image conversion is done by compression, witch is very long running operation. * * @param bmp original bitmap * @param format compress format * @param quality compress quality * @return compress image as byte array * @see android.graphics.Bitmap#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream) */ public static byte[] bmpToByte(Bitmap bmp, Bitmap.CompressFormat format, int quality) { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(format, quality, stream); try { stream.flush(); byte[] data = stream.toByteArray(); stream.close(); return data; } catch (IOException e) { return null; } }
From source file:Main.java
public static byte[] getContent(Document doc) throws Exception { ByteArrayOutputStream writer = new ByteArrayOutputStream(); transformer.transform(new DOMSource(doc), new StreamResult(new BufferedOutputStream(writer, 65536))); writer.flush(); return writer.toByteArray(); }
From source file:Main.java
public static byte[] readInputStream(InputStream is) throws IOException { byte[] buffer = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len;//from ww w. j a v a2s.c o m while ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } baos.flush(); baos.close(); is.close(); return baos.toByteArray(); }
From source file:Main.java
public static byte[] readBytes(InputStream is) throws IOException { try {/*ww w. ja v a2s . c o m*/ BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int ch; while ((ch = bis.read()) >= 0) { bos.write(ch); } bos.flush(); return bos.toByteArray(); } finally { is.close(); } }
From source file:Main.java
public static byte[] bitmap2Bytes(Bitmap bm) { if (bm == null || bm.isRecycled()) { return null; }/*from w ww.j av a 2 s . c o m*/ byte[] bytes; ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); bytes = baos.toByteArray(); try { baos.flush(); baos.close(); } catch (IOException e) { e.printStackTrace(); } return bytes; }
From source file:Main.java
static byte[] compress(byte[] decompressed) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(bos); gzip.write(decompressed);/*from ww w.j a va 2 s . com*/ gzip.flush(); gzip.close(); bos.flush(); bos.close(); return bos.toByteArray(); }
From source file:Main.java
public static byte[] bitmap2Bytes(Bitmap bitmap) { ByteArrayOutputStream baos; try {//from ww w .j a va 2 s . c o m baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] b = baos.toByteArray(); baos.flush(); baos.close(); return b; } catch (Exception e) { e.printStackTrace(); } return null; }