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

public static byte[] resize(byte[] picArray, int size, boolean faceDetect) {
    if (picArray == null) {
        return null;
    }/*from   ww  w.j a v  a 2s.c om*/
    Bitmap pic = BitmapFactory.decodeByteArray(picArray, 0, picArray.length);
    if (pic == null) {
        return null;
    }

    size = Math.min(size, Math.min(pic.getHeight(), pic.getWidth()));
    if (size % 2 != 0)
        size--;
    Log.i("sizes", "old width:" + pic.getWidth() + " old height:" + pic.getHeight() + " new size:" + size);
    Bitmap scaledPic = scale(pic, size);

    int width = scaledPic.getWidth();
    int height = scaledPic.getHeight();

    //if pic is already square, we are done now
    if (width == height) {
        return bitmapToBytes(scaledPic);
    }

    PointF mid = null;
    int cropcenter;

    if (faceDetect)
        mid = findFaceMid(scaledPic);

    Bitmap out;
    if (width > height) {
        if (mid != null)
            cropcenter = Math.max(size / 2, Math.min((int) Math.floor(mid.y), width - size / 2));
        else
            cropcenter = width / 2;
        Log.i("CROPPING", "width:" + width + " center:" + cropcenter + " size:" + size + " left edge:"
                + (cropcenter - size / 2) + " right edge:" + (cropcenter + size / 2));
        out = Bitmap.createBitmap(scaledPic, cropcenter - size / 2, 0, size, size);
    } else {
        if (mid != null)
            cropcenter = Math.max(size / 2, Math.min((int) Math.floor(mid.x), height - size / 2));
        else
            cropcenter = height / 2;
        out = Bitmap.createBitmap(scaledPic, 0, 0, size, size);
    }

    return bitmapToBytes(out);
}

From source file:Main.java

/**
 * Load Bitmap from a url//  w  w w .  j ava 2s. c om
 * @param url   bitmap url to be loaded
 * @return Bitmap object, null if could not be loaded
 */
public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream is = null;
    BufferedOutputStream bos = null;
    try {
        is = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bos = new BufferedOutputStream(baos, IO_BUFFER_SIZE);
        copyStream(is, bos);
        bos.flush();
        final byte[] data = baos.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e(TAG, "IOException in loadBitmap");
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        // TODO Auto-generated catch block
        Log.e(TAG, "OutOfMemoryError in loadBitmap");
        e.printStackTrace();
    } finally {
        closeStream(is);
        closeStream(bos);
    }
    return bitmap;
}

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 ww .j  av  a2  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();
        //            BitmapFactory.Options options = new BitmapFactory.Options();
        //            options.inSampleSize = 2;
        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;
}

From source file:Main.java

public static Bitmap decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp, int width, int height) {
    final int frameSize = width * height;
    if (rgbBuf == null)
        throw new NullPointerException("buffer 'rgbBuf' is null");
    if (rgbBuf.length < frameSize * 3)
        throw new IllegalArgumentException(
                "buffer 'rgbBuf' size " + rgbBuf.length + " < minimum " + frameSize * 3);

    if (yuv420sp == null)
        throw new NullPointerException("buffer 'yuv420sp' is null");

    if (yuv420sp.length < frameSize * 3 / 2)
        throw new IllegalArgumentException(
                "buffer 'yuv420sp' size " + yuv420sp.length + " < minimum " + frameSize * 3 / 2);

    int i = 0, y = 0;
    int uvp = 0, u = 0, v = 0;
    int y1192 = 0, r = 0, g = 0, b = 0;

    for (int j = 0, yp = 0; j < height; j++) {
        uvp = frameSize + (j >> 1) * width;
        u = 0;//from   w ww .  j  a v a2  s  .com
        v = 0;
        for (i = 0; i < width; i++, yp++) {
            y = (0xff & ((int) yuv420sp[yp])) - 16;
            if (y < 0)
                y = 0;
            if ((i & 1) == 0) {
                v = (0xff & yuv420sp[uvp++]) - 128;
                u = (0xff & yuv420sp[uvp++]) - 128;
            }

            y1192 = 1192 * y;
            r = (y1192 + 1634 * v);
            g = (y1192 - 833 * v - 400 * u);
            b = (y1192 + 2066 * u);

            if (r < 0)
                r = 0;
            else if (r > 262143)
                r = 262143;
            if (g < 0)
                g = 0;
            else if (g > 262143)
                g = 262143;
            if (b < 0)
                b = 0;
            else if (b > 262143)
                b = 262143;

            rgbBuf[yp * 3] = (byte) (r >> 10);
            rgbBuf[yp * 3 + 1] = (byte) (g >> 10);
            rgbBuf[yp * 3 + 2] = (byte) (b >> 10);
        }
    }
    return BitmapFactory.decodeByteArray(rgbBuf, 0, rgbBuf.length);
}

From source file:com.royclarkson.springagram.model.ItemResource.java

public void setImage(String imageDataUri) {
    String imageDataString = imageDataUri.substring(imageDataUri.indexOf(",") + 1);
    byte[] imageData = Base64.decode(imageDataString, Base64.DEFAULT);
    Bitmap original = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
    this.image = ThumbnailUtils.extractThumbnail(original, IMAGE_WIDTH, IMAGE_HEIGHT);
    this.thumbnail = ThumbnailUtils.extractThumbnail(original, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
}

From source file:mr.robotto.engine.loader.file.MrMrrLoader.java

private Bitmap loadTextureData() throws IOException {
    int numBytes = readInt32BE();
    readEOL();//from  w ww  .  ja v a 2  s. c o  m
    byte[] image = new byte[numBytes];
    mStream.readFully(image);
    readEOL();
    Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
    return bitmap;
}

From source file:com.gsoc.ijosa.liquidgalaxycontroller.PW.Utils.java

public static Bitmap getBitmapIcon(PhysicalWebCollection pwCollection, PwsResult pwsResult) {
    byte[] iconBytes = pwCollection.getIcon(pwsResult.getIconUrl());
    if (iconBytes == null) {
        return null;
    }/*from  ww w.  j a va  2  s .c o m*/
    return BitmapFactory.decodeByteArray(iconBytes, 0, iconBytes.length);
}

From source file:com.HumanDecisionSupportSystemsLaboratory.DD_P2P.ImageFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    iv = new ImageView(getActivity());
    byte[] icon = getArguments().getByteArray(EXTRA_IMAGE_BYTE_ARRAY);
    Bitmap bmp = BitmapFactory.decodeByteArray(icon, 0, icon.length - 1);
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    iv.setLayoutParams(params);// ww w. j  a va2s.c  o m
    iv.setImageBitmap(bmp);
    return iv;
}

From source file:com.cnm.cnmrc.fragment.search.SearchVodDetail.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View layout = inflater.inflate(R.layout.search_vod_detail, container, false);

    Bundle bundle = getArguments().getBundle("bundle");

    byte[] logoImage = bundle.getByteArray("logoImg");
    String title = bundle.getString("title");
    String hd = bundle.getString("hd");
    String grade = bundle.getString("grade");
    String director = bundle.getString("director");
    String actor = bundle.getString("actor");
    String price = bundle.getString("price");
    String contents = bundle.getString("contents");

    Bitmap bmp = BitmapFactory.decodeByteArray(logoImage, 0, logoImage.length);
    ImageView logoImg = (ImageView) layout.findViewById(R.id.logo_img);
    logoImg.setImageBitmap(bmp);//from w  w w. j av  a 2 s.  c o m

    if (title != null)
        ((TextView) layout.findViewById(R.id.title)).setText(title);
    if (hd != null) {
        if (hd.equalsIgnoreCase("yes"))
            ((ImageView) layout.findViewById(R.id.hd_icon)).setVisibility(View.VISIBLE);
    }
    if (grade != null)
        ((ImageView) layout.findViewById(R.id.grade_icon)).setBackgroundResource(Util.getGrade(grade));
    if (director != null)
        ((TextView) layout.findViewById(R.id.director_name)).setText(" " + director);
    if (actor != null)
        ((TextView) layout.findViewById(R.id.actor_name)).setText(" " + actor);
    if (grade != null)
        ((TextView) layout.findViewById(R.id.grade_text)).setText(" " + grade);
    if (price != null)
        ((TextView) layout.findViewById(R.id.price_amount)).setText(" " + price);
    if (contents != null)
        ((TextView) layout.findViewById(R.id.contents)).setText(contents);

    return layout;
}

From source file:Main.java

@SuppressLint("NewApi")
public static Bitmap NV21ToRGBABitmap(byte[] nv21, int width, int height, Context context) {

    TimingLogger timings = new TimingLogger(TIMING_LOG_TAG, "NV21ToRGBABitmap");

    Rect rect = new Rect(0, 0, width, height);

    try {/*w  w  w . j  a v  a  2s  . c  om*/
        Class.forName("android.renderscript.Element$DataKind").getField("PIXEL_YUV");
        Class.forName("android.renderscript.ScriptIntrinsicYuvToRGB");
        byte[] imageData = nv21;
        if (mRS == null) {
            mRS = RenderScript.create(context);
            mYuvToRgb = ScriptIntrinsicYuvToRGB.create(mRS, Element.U8_4(mRS));
            Type.Builder tb = new Type.Builder(mRS,
                    Element.createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
            tb.setX(width);
            tb.setY(height);
            tb.setMipmaps(false);
            tb.setYuvFormat(ImageFormat.NV21);
            ain = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
            timings.addSplit("Prepare for ain");
            Type.Builder tb2 = new Type.Builder(mRS, Element.RGBA_8888(mRS));
            tb2.setX(width);
            tb2.setY(height);
            tb2.setMipmaps(false);
            aOut = Allocation.createTyped(mRS, tb2.create(), Allocation.USAGE_SCRIPT & Allocation.USAGE_SHARED);
            timings.addSplit("Prepare for aOut");
            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            timings.addSplit("Create Bitmap");
        }
        ain.copyFrom(imageData);
        timings.addSplit("ain copyFrom");
        mYuvToRgb.setInput(ain);
        timings.addSplit("setInput ain");
        mYuvToRgb.forEach(aOut);
        timings.addSplit("NV21 to ARGB forEach");
        aOut.copyTo(bitmap);
        timings.addSplit("Allocation to Bitmap");
    } catch (Exception e) {
        YuvImage yuvImage = new YuvImage(nv21, ImageFormat.NV21, width, height, null);
        timings.addSplit("NV21 bytes to YuvImage");

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        yuvImage.compressToJpeg(rect, 90, baos);
        byte[] cur = baos.toByteArray();
        timings.addSplit("YuvImage crop and compress to Jpeg Bytes");

        bitmap = BitmapFactory.decodeByteArray(cur, 0, cur.length);
        timings.addSplit("Jpeg Bytes to Bitmap");
    }

    timings.dumpToLog();
    return bitmap;
}