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:com.cmput301w15t15.travelclaimsapp.activitys.EditExpenseActivity.java

/**
        //www . j  av a2  s. co  m
 * Returns given bitmap as a byte array. Rescales large images to correct size.
 * 
 * @param bitmap
 * @return byte[]
 */
public byte[] getBytesFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
    byte[] tempImg;
    Bitmap tempMap = bitmap;
    Bitmap resizedMap;

    double initWidth = Double.valueOf(bitmap.getWidth());
    double initHeight = Double.valueOf(bitmap.getHeight());

    double resizeWidth = initWidth;
    double resizeHeight = initHeight;

    while (resizeWidth * resizeHeight >= 65536) {
        resizeWidth = resizeWidth * 0.9;
        resizeHeight = resizeHeight * 0.9;
    }

    tempMap.compress(CompressFormat.JPEG, 70, stream2);
    tempImg = stream2.toByteArray();
    if (tempImg.length > 65536) {
        resizedMap = Bitmap.createScaledBitmap(bitmap, (int) resizeWidth, (int) resizeHeight, false);
        resizedMap.compress(CompressFormat.JPEG, 100, stream);
        tempImg = stream.toByteArray();
        expense.setScale("Rescaled");
    }

    else {
        resizedMap = Bitmap.createScaledBitmap(tempMap, (int) resizeWidth, (int) resizeHeight, false);
        resizedMap.compress(CompressFormat.JPEG, 70, stream);
        tempImg = stream.toByteArray();
        expense.setScale("Original");
    }

    sizeNum = tempImg.length;
    size = sizeNum.toString();

    if (sizeNum > 65536) {
        Toast.makeText(this, "Image Too Large", Toast.LENGTH_LONG).show();
        return null;
    } else {
        return tempImg;
    }

}

From source file:com.freshplanet.ane.GoogleCloudStorageUpload.tasks.UploadToGoogleCloudStorageAsyncTask.java

public static Bitmap resizeImage(Bitmap image, int maxWidth, int maxHeight) {
    Log.d(TAG, "[AirImagePickerExtensionContext] Entering resizeImage - maxWidth: " + maxWidth
            + " - maxHeight: " + maxHeight);
    Bitmap result = image;//from   w  w  w  .  ja  v  a2 s  .  c  o  m
    // make sure that the image has the correct height
    if ((image.getWidth() > maxWidth || image.getHeight() > maxHeight) && maxWidth != -1 && maxHeight != -1) {
        float reductionFactor = Math.max(image.getWidth() / maxWidth, image.getHeight() / maxHeight);

        result = Bitmap.createScaledBitmap(image, (int) (image.getWidth() / reductionFactor),
                (int) (image.getHeight() / reductionFactor), true);
        Log.d(TAG, "[AirImagePickerExtensionContext] resized image");
    }
    Log.d(TAG, "[AirImagePickerExtensionContext] Exiting resizeImage");
    return result;
}

From source file:com.truebanana.bitmap.BitmapUtils.java

@SuppressLint("NewApi")
public static Bitmap blur(Context context, Bitmap bitmap, float scale, int radius) {
    if (SystemUtils.getSDKVersion() >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        int width = Math.round(bitmap.getWidth() * scale);
        int height = Math.round(bitmap.getHeight() * scale);

        bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
        Bitmap processedBitmap = Bitmap.createBitmap(bitmap);

        RenderScript rs = RenderScript.create(context);
        ScriptIntrinsicBlur sib = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation tmpIn = Allocation.createFromBitmap(rs, bitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, processedBitmap);
        sib.setRadius(radius);//from w w w.  j  a va 2s  . c  o  m
        sib.setInput(tmpIn);
        sib.forEach(tmpOut);
        tmpOut.copyTo(processedBitmap);

        return processedBitmap;
    } else {
        int width = Math.round(bitmap.getWidth() * scale);
        int height = Math.round(bitmap.getHeight() * scale);
        bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);

        Bitmap processedBitmap = bitmap.copy(bitmap.getConfig(), true);

        if (radius < 1) {
            return null;
        }

        int w = processedBitmap.getWidth();
        int h = processedBitmap.getHeight();

        int[] pix = new int[w * h];
        processedBitmap.getPixels(pix, 0, w, 0, 0, w, h);

        int wm = w - 1;
        int hm = h - 1;
        int wh = w * h;
        int div = radius + radius + 1;

        int r[] = new int[wh];
        int g[] = new int[wh];
        int b[] = new int[wh];
        int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
        int vmin[] = new int[Math.max(w, h)];

        int divsum = (div + 1) >> 1;
        divsum *= divsum;
        int dv[] = new int[256 * divsum];
        for (i = 0; i < 256 * divsum; i++) {
            dv[i] = (i / divsum);
        }

        yw = yi = 0;

        int[][] stack = new int[div][3];
        int stackpointer;
        int stackstart;
        int[] sir;
        int rbs;
        int r1 = radius + 1;
        int routsum, goutsum, boutsum;
        int rinsum, ginsum, binsum;

        for (y = 0; y < h; y++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            for (i = -radius; i <= radius; i++) {
                p = pix[yi + Math.min(wm, Math.max(i, 0))];
                sir = stack[i + radius];
                sir[0] = (p & 0xff0000) >> 16;
                sir[1] = (p & 0x00ff00) >> 8;
                sir[2] = (p & 0x0000ff);
                rbs = r1 - Math.abs(i);
                rsum += sir[0] * rbs;
                gsum += sir[1] * rbs;
                bsum += sir[2] * rbs;
                if (i > 0) {
                    rinsum += sir[0];
                    ginsum += sir[1];
                    binsum += sir[2];
                } else {
                    routsum += sir[0];
                    goutsum += sir[1];
                    boutsum += sir[2];
                }
            }
            stackpointer = radius;

            for (x = 0; x < w; x++) {
                r[yi] = dv[rsum];
                g[yi] = dv[gsum];
                b[yi] = dv[bsum];

                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;

                stackstart = stackpointer - radius + div;
                sir = stack[stackstart % div];

                routsum -= sir[0];
                goutsum -= sir[1];
                boutsum -= sir[2];

                if (y == 0) {
                    vmin[x] = Math.min(x + radius + 1, wm);
                }
                p = pix[yw + vmin[x]];

                sir[0] = (p & 0xff0000) >> 16;
                sir[1] = (p & 0x00ff00) >> 8;
                sir[2] = (p & 0x0000ff);

                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];

                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;

                stackpointer = (stackpointer + 1) % div;
                sir = stack[(stackpointer) % div];

                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];

                rinsum -= sir[0];
                ginsum -= sir[1];
                binsum -= sir[2];

                yi++;
            }
            yw += w;
        }
        for (x = 0; x < w; x++) {
            rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
            yp = -radius * w;
            for (i = -radius; i <= radius; i++) {
                yi = Math.max(0, yp) + x;

                sir = stack[i + radius];

                sir[0] = r[yi];
                sir[1] = g[yi];
                sir[2] = b[yi];

                rbs = r1 - Math.abs(i);

                rsum += r[yi] * rbs;
                gsum += g[yi] * rbs;
                bsum += b[yi] * rbs;

                if (i > 0) {
                    rinsum += sir[0];
                    ginsum += sir[1];
                    binsum += sir[2];
                } else {
                    routsum += sir[0];
                    goutsum += sir[1];
                    boutsum += sir[2];
                }

                if (i < hm) {
                    yp += w;
                }
            }
            yi = x;
            stackpointer = radius;
            for (y = 0; y < h; y++) {
                // Preserve alpha channel: ( 0xff000000 & pix[yi] )
                pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

                rsum -= routsum;
                gsum -= goutsum;
                bsum -= boutsum;

                stackstart = stackpointer - radius + div;
                sir = stack[stackstart % div];

                routsum -= sir[0];
                goutsum -= sir[1];
                boutsum -= sir[2];

                if (x == 0) {
                    vmin[y] = Math.min(y + r1, hm) * w;
                }
                p = x + vmin[y];

                sir[0] = r[p];
                sir[1] = g[p];
                sir[2] = b[p];

                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];

                rsum += rinsum;
                gsum += ginsum;
                bsum += binsum;

                stackpointer = (stackpointer + 1) % div;
                sir = stack[stackpointer];

                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];

                rinsum -= sir[0];
                ginsum -= sir[1];
                binsum -= sir[2];

                yi += w;
            }
        }
        processedBitmap.setPixels(pix, 0, w, 0, 0, w, h);

        return processedBitmap;
    }
}

From source file:moe.johnny.tombstone.ui.PreventFragment.java

private void setHeaderIcon(ContextMenu menu, Drawable icon) {
    int width = getHeaderIconWidth();
    if (icon.getMinimumWidth() <= width) {
        menu.setHeaderIcon(icon);//from w w w.  jav a2s  .  co m
    } else if (icon instanceof BitmapDrawable) {
        Bitmap bitmap = Bitmap.createScaledBitmap(((BitmapDrawable) icon).getBitmap(), width, width, false);
        menu.setHeaderIcon(new BitmapDrawable(getResources(), bitmap));
    }
}

From source file:com.pimp.companionforband.fragments.theme.ThemeFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 9) {
        try {/*  w w w .  j a v  a 2 s.  c  o m*/
            final Uri imageUri = data.getData();
            final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
            final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
            if (MainActivity.band2)
                imageView.setImageBitmap(Bitmap.createScaledBitmap(selectedImage, 310, 128, false));
            else
                imageView.setImageBitmap(Bitmap.createScaledBitmap(selectedImage, 310, 102, false));
        } catch (Exception e) {
            //
        }
    }
}

From source file:com.hivewallet.androidclient.wallet.service.BlockchainServiceImpl.java

private void notifyCoinsReceived(@Nullable final Address from, @Nonnull final BigInteger amount) {
    final int btcPrecision = config.getBtcPrecision();
    final int btcShift = config.getBtcShift();
    final String btcPrefix = config.getBtcPrefix();

    final String packageFlavor = application.applicationPackageFlavor();
    final String msgSuffix = packageFlavor != null ? " [" + packageFlavor + "]" : "";

    String label = null;/*from w  ww.j a v a  2s. c om*/
    Uri photoUri = null;
    Bitmap photoBitmap = null;
    if (from != null) {
        final String addressStr = from.toString();
        AddressBookEntry entry = AddressBookProvider.lookupEntry(getApplicationContext(), addressStr);
        if (entry != null) {
            label = entry.getLabel();
            photoUri = entry.getPhotoUri();
        }
        if (photoUri != null) {
            try {
                Bitmap photoBitmapOriginal = MediaStore.Images.Media.getBitmap(getContentResolver(), photoUri);
                int width = (int) getResources().getDimension(android.R.dimen.notification_large_icon_width);
                int height = (int) getResources().getDimension(android.R.dimen.notification_large_icon_height);
                photoBitmap = Bitmap.createScaledBitmap(photoBitmapOriginal, width, height, false);
            } catch (FileNotFoundException ignored) {
            } catch (IOException ignored) {
            }
        }
    }

    final StringBuilder tickerMsg = new StringBuilder();
    if (label != null)
        tickerMsg.append(getString(R.string.notification_coins_received_with_label_msg,
                GenericUtils.formatValue(amount, btcPrecision, btcShift) + ' ' + btcPrefix, label));
    else
        tickerMsg.append(getString(R.string.notification_coins_received_msg,
                GenericUtils.formatValue(amount, btcPrecision, btcShift) + ' ' + btcPrefix));
    tickerMsg.append(msgSuffix);

    String contentTitle = getString(R.string.notification_coins_received_no_address);
    if (from != null)
        contentTitle = from.toString();
    if (label != null)
        contentTitle = label;

    final String contentText = getString(R.string.notification_coins_received_msg,
            GenericUtils.formatValue(amount, btcPrecision, btcShift) + ' ' + btcPrefix) + msgSuffix;

    final NotificationCompat.Builder notification = new NotificationCompat.Builder(this);
    notification.setSmallIcon(R.drawable.app_icon);
    notification.setTicker(tickerMsg);
    if (photoBitmap != null)
        notification.setLargeIcon(photoBitmap);
    notification.setContentTitle(contentTitle);
    notification.setContentText(contentText);
    notification
            .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, WalletActivity.class), 0));
    notification.setNumber(0);
    notification.setWhen(System.currentTimeMillis());
    notification.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.coins_received));
    nm.notify(NOTIFICATION_ID_COINS_RECEIVED, notification.getNotification());
}

From source file:com.vendsy.bartsy.venue.BartsyApplication.java

private void generateNotification(final String title, final String body, final int count) {
    mHandler.post(new Runnable() {
        public void run() {
            // Get app icon bitmap and scale to fit in notification
            Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
            largeIcon = Bitmap.createScaledBitmap(largeIcon, NOTIFICATION_IMAGE_SIZE, NOTIFICATION_IMAGE_SIZE,
                    true);//from  w ww . ja va 2s  .com

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
                    .setLargeIcon(largeIcon).setSmallIcon(R.drawable.ic_launcher).setContentTitle(title)
                    .setContentText(body);

            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);

            // The stack builder object will contain an artificial back stack for the
            // started Activity.
            // This ensures that navigating backward from the Activity leads out of
            // your application to the Home screen.
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(MainActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
            mBuilder.setNumber(count);
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                    Context.NOTIFICATION_SERVICE);

            // Play default notification sound
            mBuilder.setDefaults(Notification.DEFAULT_SOUND);
            // mId allows you to update the notification later on.
            mNotificationManager.notify(0, mBuilder.build());
        }
    });
}

From source file:com.rexmtorres.android.patternlock.PatternLockView.java

@SuppressWarnings("deprecation")
public PatternLockView(Context context, AttributeSet attrs) {
    super(context, attrs);

    mContext = getContext();/*from  ww w  .jav a  2 s  .  co m*/

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PatternLockView,
            R.attr.patternLockViewStyle, 0);
    final String aspect = a.getString(R.styleable.PatternLockView_aspect);
    if ("square".equals(aspect)) {
        mAspect = ASPECT_SQUARE;
    } else if ("lock_width".equals(aspect)) {
        mAspect = ASPECT_LOCK_WIDTH;
    } else if ("lock_height".equals(aspect)) {
        mAspect = ASPECT_LOCK_HEIGHT;
    } else {
        mAspect = ASPECT_SQUARE;
    }
    setClickable(true);
    mPathPaint.setAntiAlias(true);
    mPathPaint.setDither(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mRegularColor = context.getColor(R.color.lock_pattern_view_regular_color);
        mErrorColor = context.getColor(R.color.lock_pattern_view_error_color);
        mSuccessColor = context.getColor(R.color.lock_pattern_view_success_color);
    } else {
        Resources resources = context.getResources();

        mRegularColor = resources.getColor(R.color.lock_pattern_view_regular_color);
        mErrorColor = resources.getColor(R.color.lock_pattern_view_error_color);
        mSuccessColor = resources.getColor(R.color.lock_pattern_view_success_color);
    }

    mRegularColor = a.getColor(R.styleable.PatternLockView_regularColor, mRegularColor);
    mErrorColor = a.getColor(R.styleable.PatternLockView_errorColor, mErrorColor);
    mSuccessColor = a.getColor(R.styleable.PatternLockView_successColor, mSuccessColor);

    int pathColor = a.getColor(R.styleable.PatternLockView_pathColor, mRegularColor);

    // [START rexmtorres 20160401] If set, replaces the pattern dots with the specified bitmap.
    Drawable oDotDrawable = a.getDrawable(R.styleable.PatternLockView_dotBitmap);

    if (oDotDrawable != null) {
        if (oDotDrawable instanceof BitmapDrawable) {
            m_oDotBitmap = ((BitmapDrawable) oDotDrawable).getBitmap();
            m_oBigDotBitmap = Bitmap.createScaledBitmap(m_oDotBitmap, (int) (m_oDotBitmap.getWidth() * 1.25),
                    (int) (m_oDotBitmap.getHeight() * 1.25), false);
        }
    }
    // [END rexmtorres 20160401]

    a.recycle();

    mPathPaint.setColor(pathColor);
    mPathPaint.setStyle(Paint.Style.STROKE);
    mPathPaint.setStrokeJoin(Paint.Join.ROUND);
    mPathPaint.setStrokeCap(Paint.Cap.ROUND);
    mPathWidth = getResources().getDimensionPixelSize(R.dimen.lock_pattern_dot_line_width);
    mPathPaint.setStrokeWidth(mPathWidth);
    mDotSize = getResources().getDimensionPixelSize(R.dimen.lock_pattern_dot_size);
    mDotSizeActivated = getResources().getDimensionPixelSize(R.dimen.lock_pattern_dot_size_activated);
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mCellStates = new CellState[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            mCellStates[i][j] = new CellState();
            mCellStates[i][j].radius = mDotSize / 2;
            mCellStates[i][j].row = i;
            mCellStates[i][j].col = j;
            mCellStates[i][j].bitmapDot = m_oDotBitmap;
        }
    }

    mFastOutSlowInInterpolator = new FastOutSlowInInterpolator();
    mLinearOutSlowInInterpolator = new LinearOutSlowInInterpolator();
    mExploreByTouchHelper = new PatternExploreByTouchHelper(this);
    ViewCompat.setAccessibilityDelegate(this, mExploreByTouchHelper);
    mAccessibilityManager = (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
    mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
}

From source file:freed.viewer.stack.StackActivity.java

private void setBitmapToImageView(final Bitmap bitmap) {
    runOnUiThread(new Runnable() {
        @Override//w  w w . jav  a  2 s  .  c om
        public void run() {
            imageView.setImageBitmap(
                    Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / 4, bitmap.getHeight() / 4, false));
        }
    });
}

From source file:com.github.topbottomsnackbar.TBSnackbar.java

@Deprecated
private TBSnackbar addIcon(int resource_id, int size) {
    final TextView tv = mView.getMessageView();

    tv.setCompoundDrawablesWithIntrinsicBounds(new BitmapDrawable(Bitmap.createScaledBitmap(
            ((BitmapDrawable) (mContext.getResources().getDrawable(resource_id))).getBitmap(), size, size,
            true)), null, null, null);/*  ww  w.  j a va  2 s.c  o  m*/

    return this;
}