List of usage examples for java.io ByteArrayOutputStream toByteArray
public synchronized byte[] toByteArray()
From source file:Main.java
public static byte[] toBytes(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int ch;//from w ww .ja v a 2s .co m while ((ch = in.read()) != -1) { out.write(ch); } byte buffer[] = out.toByteArray(); out.close(); return buffer; }
From source file:Main.java
public static Bitmap yuv2Bitmap(byte[] data, int width, int height) { final YuvImage image = new YuvImage(data, ImageFormat.NV21, width, height, null); ByteArrayOutputStream os = new ByteArrayOutputStream(data.length); if (!image.compressToJpeg(new Rect(0, 0, width, height), 100, os)) { return null; }//from w w w . ja v a2 s . c om byte[] tmp = os.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(tmp, 0, tmp.length); return bitmap; }
From source file:Main.java
public static byte[] getBytes(InputStream is) throws IOException { ByteArrayOutputStream os = null; try {/*ww w.j a v a 2 s . com*/ os = new ByteArrayOutputStream(); copyStream(is, os); } finally { os.close(); } return os.toByteArray(); }
From source file:Main.java
public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat mCompressFormat, final boolean needRecycle) { byte[] result = null; ByteArrayOutputStream output = null; try {// w w w. ja va 2s . c o m output = new ByteArrayOutputStream(); bitmap.compress(mCompressFormat, 100, output); result = output.toByteArray(); if (needRecycle) { bitmap.recycle(); } } catch (Exception e) { e.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (Exception e) { e.printStackTrace(); } } } return result; }
From source file:Main.java
public static InputStream convertMessageToInputStream(Source src) throws IOException, TransformerConfigurationException, TransformerException { final Transformer transformer = TRANSFORMER_FACTORY.newTransformer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(baos); transformer.transform(src, result);//from w ww . j ava 2 s . c om return new ByteArrayInputStream(baos.toByteArray()); }
From source file:Main.java
public static byte[] getCompressedImage(byte[] rgb) { BufferedImage image;//from ww w . java 2s . 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 byte[] bitmap2ByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, output); if (needRecycle) { bmp.recycle();/* w ww. j a v a 2s .c o m*/ } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
/** * GZip a given byte-array in-memory.//from w w w . j a va 2 s. c o m * * @param bytes * the uncompressed bytes * * @return the GZipped byte stream * * @throws IOException * if something fails */ public static byte[] gzipByteArray(byte[] bytes) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length); GZIPOutputStream gzip = new GZIPOutputStream(baos); gzip.write(bytes); gzip.close(); return baos.toByteArray(); }
From source file:Main.java
static public Bitmap NV21ToRGBABitmap(byte[] nv21, int width, int height) { YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, baos); byte[] cur = baos.toByteArray(); return BitmapFactory.decodeByteArray(cur, 0, cur.length); }
From source file:Main.java
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { if (bmp == null || bmp.isRecycled()) return null; ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, output); if (needRecycle) { bmp.recycle();//www. j a v a2 s .c om } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; }