Example usage for android.graphics BitmapFactory decodeByteArray

List of usage examples for android.graphics BitmapFactory decodeByteArray

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeByteArray.

Prototype

public static Bitmap decodeByteArray(byte[] data, int offset, int length) 

Source Link

Document

Decode an immutable bitmap from the specified byte array.

Usage

From source file:Main.java

/**
 * Decodes an image byte[] to Bitmap without resizing the image.<br>
 * Be careful, if the image is big this can become a problem because it will use to much memory.
 * @param imageBytes image content in byte[]
 * @return the Bitmap/*from   www .j a v a  2s . c om*/
 */
public static Bitmap decodeBitmapFromByteArray(byte[] imageBytes) {
    return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
}

From source file:Main.java

public static Bitmap decodeBase64(String input) {
    byte[] decodedByte = Base64.decode(input, 0);
    return bitmapResult = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

From source file:Main.java

public static Bitmap createVideoThumbnail(String filePath) {
    // MediaMetadataRetriever is available on API Level 8
    // but is hidden until API Level 10
    Class<?> clazz = null;/*from w  w w. j  av a  2s .c  o  m*/
    Object instance = null;
    try {
        clazz = Class.forName("android.media.MediaMetadataRetriever");
        instance = clazz.newInstance();

        Method method = clazz.getMethod("setDataSource", String.class);
        method.invoke(instance, filePath);

        // The method name changes between API Level 9 and 10.
        if (Build.VERSION.SDK_INT <= 9) {
            return (Bitmap) clazz.getMethod("captureFrame").invoke(instance);
        } else {
            byte[] data = (byte[]) clazz.getMethod("getEmbeddedPicture").invoke(instance);
            if (data != null) {
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                if (bitmap != null)
                    return bitmap;
            }
            return (Bitmap) clazz.getMethod("getFrameAtTime").invoke(instance);
        }
    } catch (IllegalArgumentException ex) {
        // Assume this is a corrupt video file
    } catch (RuntimeException ex) {
        // Assume this is a corrupt video file.
    } catch (InstantiationException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (InvocationTargetException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (ClassNotFoundException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (NoSuchMethodException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } catch (IllegalAccessException e) {
        Log.e(TAG, "createVideoThumbnail", e);
    } finally {
        try {
            if (instance != null) {
                clazz.getMethod("release").invoke(instance);
            }
        } catch (Exception ignored) {
        }
    }
    return null;
}

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 ww  . jav  a 2  s. c  o m
    byte[] tmp = os.toByteArray();
    Bitmap bitmap = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);
    return bitmap;
}

From source file:Main.java

public static Bitmap scaleToFillBitmap(Bitmap dst, byte[] byImage) {
    Bitmap src = BitmapFactory.decodeByteArray(byImage, 0, byImage.length);

    float scaled = 1.0f;
    if ((float) dst.getWidth() / (float) src.getWidth() < (float) dst.getHeight() / (float) src.getHeight()) {
        scaled = (float) dst.getHeight() / (float) src.getHeight();
    } else {/*from   ww w.  java 2s.c o  m*/
        scaled = (float) dst.getWidth() / (float) src.getWidth();
    }

    Bitmap bmpScaled = Bitmap.createScaledBitmap(src, (int) Math.ceil(src.getWidth() * scaled),
            (int) Math.ceil(src.getHeight() * scaled), true);

    int offsetX = 0;
    int offsetY = 0;
    offsetX = bmpScaled.getWidth() - dst.getWidth() != 0 ? (bmpScaled.getWidth() - dst.getWidth()) / 2 : 0;
    offsetY = bmpScaled.getHeight() - dst.getHeight() != 0 ? (bmpScaled.getHeight() - dst.getHeight()) / 2 : 0;

    return Bitmap.createBitmap(bmpScaled, offsetX, offsetY, dst.getWidth(), dst.getHeight());
}

From source file:Main.java

/**
 * Gets the bitmap image.//from   ww w. java  2s . co  m
 *
 * @param context the context
 * @param base64  the base64
 * @return the bitmap image
 */
public static Bitmap getBitmapImage(Context context, String base64) {
    byte[] imageAsBytes = Base64.decode(base64.getBytes(), 5);
    return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

}

From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.VideoObj.java

public void render(Context context, ViewGroup frame, Obj obj, boolean allowInteractions) {
    JSONObject content = obj.getJson();//w  w w .  ja v  a 2  s  . c  o m
    byte[] raw = obj.getRaw();

    if (raw == null) {
        Pair<JSONObject, byte[]> p = splitRaw(content);
        content = p.first;
        raw = p.second;
    }

    LinearLayout inner = new LinearLayout(context);
    inner.setLayoutParams(CommonLayouts.FULL_WIDTH);
    inner.setOrientation(LinearLayout.HORIZONTAL);
    frame.addView(inner);

    ImageView imageView = new ImageView(context);
    imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    BitmapFactory bf = new BitmapFactory();
    imageView.setImageBitmap(bf.decodeByteArray(raw, 0, raw.length));
    inner.addView(imageView);

    ImageView iconView = new ImageView(context);
    iconView.setImageResource(R.drawable.play);
    iconView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    inner.addView(iconView);
}

From source file:Main.java

/**
 * Gets the bitmap image.//ww w .  j a  v  a2s. c o  m
 *
 * @param context the context
 * @param base64  the base64
 * @return the bitmap image
 */
public static Bitmap getBitmapImage(Context context, String base64) {

    String imagestring = base64;
    byte[] imageAsBytes = Base64.decode(imagestring.getBytes(), 5);
    return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

}

From source file:Main.java

public static Bitmap bytesToBitmap(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
        return null;
    }/* ww  w. j a  v a  2 s  . c  om*/
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}

From source file:Main.java

/**
 * Loads a bitmap from the specified url. This can take a while, so it should not
 * be called from the UI thread.//from   w  w w.  ja  v  a 2  s. c  o  m
 * 
 * @param url The location of the bitmap asset
 * 
 * @return The bitmap, or null if it could not be loaded
 */
public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}