Example usage for android.graphics Bitmap createScaledBitmap

List of usage examples for android.graphics Bitmap createScaledBitmap

Introduction

In this page you can find the example usage for android.graphics Bitmap createScaledBitmap.

Prototype

public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight, boolean filter) 

Source Link

Document

Creates a new bitmap, scaled from an existing bitmap, when possible.

Usage

From source file:nl.privacybarometer.privacyvandaag.utils.UiUtils.java

static public Bitmap getScaledBitmap(byte[] iconBytes, int sizeInDp) {
    if (iconBytes != null && iconBytes.length > 0) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(iconBytes, 0, iconBytes.length);
        if (bitmap != null && bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
            int bitmapSizeInDip = UiUtils.dpToPixel(sizeInDp);
            if (bitmap.getHeight() != bitmapSizeInDip) {
                Bitmap tmp = bitmap;/*from ww  w.  j a  va2 s .c  o  m*/
                bitmap = Bitmap.createScaledBitmap(tmp, bitmapSizeInDip, bitmapSizeInDip, false);
                tmp.recycle();
            }
            return bitmap;
        }
    }
    return null;
}

From source file:com.dev.pygmy.util.Utils.java

public static Bitmap getBitmapByType(Resources res, EntityType type, int scale) {
    int resId;//from  w ww  .j  av a  2 s  .  c o  m
    switch (type) {
    case BLACK_ROOK:
        resId = R.drawable.black_rook;
        break;
    case BLACK_KNIGHT:
        resId = R.drawable.black_knight;
        break;
    case BLACK_BISHOP:
        resId = R.drawable.black_bishop;
        break;
    case BLACK_QUEEN:
        resId = R.drawable.black_queen;
        break;
    case BLACK_KING:
        resId = R.drawable.black_king;
        break;
    case BLACK_PAWN:
        resId = R.drawable.black_pawn;
        break;

    case WHITE_ROOK:
        resId = R.drawable.white_rook;
        break;
    case WHITE_KNIGHT:
        resId = R.drawable.white_knight;
        break;
    case WHITE_BISHOP:
        resId = R.drawable.white_bishop;
        break;
    case WHITE_QUEEN:
        resId = R.drawable.white_queen;
        break;
    case WHITE_KING:
        resId = R.drawable.white_king;
        break;
    case WHITE_PAWN:
        resId = R.drawable.white_pawn;
        break;
    default:
        resId = -1;
        break;
    }

    Bitmap bitmap = null;
    if (resId != -1) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        bitmap = BitmapFactory.decodeResource(res, resId);
        bitmap = Bitmap.createScaledBitmap(bitmap, scale, scale, false);
    }

    return bitmap;
}

From source file:com.danimahardhika.android.helpers.core.DrawableHelper.java

@Nullable
public static Drawable getResizedDrawable(@NonNull Context context, @NonNull Drawable drawable,
        float sizeInDp) {
    try {// w  w  w . j a  va 2s.co m
        int size = Math.round(UnitHelper.toPixel(context, sizeInDp));

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, size, size, true));
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:Main.java

/**
 * Read bytes.//w w w.  j  ava  2  s  . c  o  m
 *
 * @param uri      the uri
 * @param resolver the resolver
 * @return the byte[]
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static byte[] readBytes(Uri uri, ContentResolver resolver, boolean thumbnail) throws IOException {
    // this dynamically extends to take the bytes you read
    InputStream inputStream = resolver.openInputStream(uri);
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

    if (!thumbnail) {
        // this is storage overwritten on each iteration with bytes
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        // we need to know how may bytes were read to write them to the
        // byteBuffer
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
    } else {
        Bitmap imageBitmap = BitmapFactory.decodeStream(inputStream);
        int thumb_width = imageBitmap.getWidth() / 2;
        int thumb_height = imageBitmap.getHeight() / 2;
        if (thumb_width > THUMBNAIL_SIZE) {
            thumb_width = THUMBNAIL_SIZE;
        }
        if (thumb_width == THUMBNAIL_SIZE) {
            thumb_height = ((imageBitmap.getHeight() / 2) * THUMBNAIL_SIZE) / (imageBitmap.getWidth() / 2);
        }
        imageBitmap = Bitmap.createScaledBitmap(imageBitmap, thumb_width, thumb_height, false);
        imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteBuffer);
    }
    // and then we can return your byte array.
    return byteBuffer.toByteArray();
}

From source file:Main.java

public static Bitmap rotateBitmap(String path, int orientation, int screenWidth, int screenHeight) {
    Bitmap bitmap = null;/*w w  w .  ja v  a  2s.  c om*/
    final int maxWidth = screenWidth / 2;
    final int maxHeight = screenHeight / 2;
    try {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int sourceWidth, sourceHeight;
        if (orientation == 90 || orientation == 270) {
            sourceWidth = options.outHeight;
            sourceHeight = options.outWidth;
        } else {
            sourceWidth = options.outWidth;
            sourceHeight = options.outHeight;
        }
        boolean compress = false;
        if (sourceWidth > maxWidth || sourceHeight > maxHeight) {
            float widthRatio = (float) sourceWidth / (float) maxWidth;
            float heightRatio = (float) sourceHeight / (float) maxHeight;

            options.inJustDecodeBounds = false;
            if (new File(path).length() > 512000) {
                float maxRatio = Math.max(widthRatio, heightRatio);
                options.inSampleSize = (int) maxRatio;
                compress = true;
            }
            bitmap = BitmapFactory.decodeFile(path, options);
        } else {
            bitmap = BitmapFactory.decodeFile(path);
        }
        if (orientation > 0) {
            Matrix matrix = new Matrix();
            //matrix.postScale(sourceWidth, sourceHeight);
            matrix.postRotate(orientation);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        }
        sourceWidth = bitmap.getWidth();
        sourceHeight = bitmap.getHeight();
        if ((sourceWidth > maxWidth || sourceHeight > maxHeight) && compress) {
            float widthRatio = (float) sourceWidth / (float) maxWidth;
            float heightRatio = (float) sourceHeight / (float) maxHeight;
            float maxRatio = Math.max(widthRatio, heightRatio);
            sourceWidth = (int) ((float) sourceWidth / maxRatio);
            sourceHeight = (int) ((float) sourceHeight / maxRatio);
            Bitmap bm = Bitmap.createScaledBitmap(bitmap, sourceWidth, sourceHeight, true);
            bitmap.recycle();
            return bm;
        }
    } catch (Exception e) {
    }
    return bitmap;
}

From source file:ca.uwaterloo.magic.goodhikes.AddMilestoneDialogFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RESULT_LOAD_IMG) {
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = data.getData();
            Bitmap bitmap = null;/*from   w  w  w .java2 s.c om*/
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
                bitmap = Bitmap.createScaledBitmap(bitmap, 300, 225, false);
            } catch (IOException e) {
                Log.d(LOG_TAG,
                        "Thread: " + Thread.currentThread().getId() + "; Couldn't load the image selected");
                //                    e.printStackTrace();
            }
            //                previewImage.setImageURI(selectedImage);
            previewImage.setImageBitmap(bitmap);
        }
    }
    if (requestCode == RESULT_CAPTURE_IMG) {
        if (resultCode == Activity.RESULT_OK) {
            Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
            imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 300, 225, false);
            previewImage.setImageBitmap(imageBitmap);
        }
    }
}

From source file:com.newtifry.android.NewtificationService2.java

@SuppressLint("InlinedApi")
public void onCreate() {
    super.onCreate();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        int height = (int) this.getResources().getDimension(android.R.dimen.notification_large_icon_height);
        int width = (int) this.getResources().getDimension(android.R.dimen.notification_large_icon_width);
        this.bigNotificationIcon = Bitmap.createScaledBitmap(
                BitmapFactory.decodeResource(this.getResources(), R.drawable.big_icon_notification), width,
                height, false);/*from  w w w . j av  a2 s. c o m*/
    }
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}

From source file:com.packpublishing.asynchronousandroid.chapter5.AlbumArtworkLoader.java

@Override
public Bitmap loadInBackground() {
    Log.i("AlbumListActivity", "Loading Album Art for album " + mAlbumId);
    Bitmap bitmap = null;/*from w  w w. jav a2 s . co m*/
    ContentResolver res = getContext().getContentResolver();

    if (mAlbumId != -1) {
        try {
            Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
            Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, mAlbumId);
            bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), albumArtUri);
            bitmap = Bitmap.createScaledBitmap(bitmap, 180, 180, true);
        } catch (IOException e) {
            Log.e("AlbumListActivity", "Failed to load", e);
        }
        return bitmap;
    }
    return bitmap;
}

From source file:com.packpublishing.asynchronousandroid.chapter4.AlbumArtworkLoader.java

@Override
public Bitmap loadInBackground() {

    Log.i("AlbumListActivity", "Loading Album Art for album " + mAlbumId);
    Bitmap bitmap = null;//from  w ww.jav  a  2 s  .c om
    ContentResolver res = getContext().getContentResolver();

    if (mAlbumId != -1) {
        try {
            Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
            Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, mAlbumId);
            bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), albumArtUri);
            bitmap = Bitmap.createScaledBitmap(bitmap, 180, 180, true);
        } catch (IOException e) {
            Log.e("AlbumListActivity", "Failed to load", e);
        }
        return bitmap;
    }
    return bitmap;
}

From source file:com.vk.sdk.api.httpClient.VKImageOperation.java

/**
 * Set listener for current operation/*from w  w  w.j  a va2  s . c  o m*/
 * @param listener Listener subclasses VKHTTPOperationCompleteListener
 */
public void setImageOperationListener(final VKImageOperationListener listener) {
    this.setCompleteListener(new VKOperationCompleteListener() {
        @Override
        public void onComplete() {
            if (VKImageOperation.this.state() != VKOperationState.Finished || mLastException != null) {
                listener.onError(VKImageOperation.this, generateError(mLastException));
            } else {
                byte[] response = getResponseData();
                Bitmap captchaImage = BitmapFactory.decodeByteArray(response, 0, response.length);
                if (imageDensity > 0) {
                    captchaImage = Bitmap.createScaledBitmap(captchaImage,
                            (int) (captchaImage.getWidth() * imageDensity),
                            (int) (captchaImage.getHeight() * imageDensity), true);
                }
                final Bitmap result = captchaImage;
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        listener.onComplete(VKImageOperation.this, result);
                    }
                });
            }
        }
    });
}