Example usage for android.graphics Canvas drawBitmap

List of usage examples for android.graphics Canvas drawBitmap

Introduction

In this page you can find the example usage for android.graphics Canvas drawBitmap.

Prototype

public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint) 

Source Link

Document

Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle.

Usage

From source file:Main.java

public static Bitmap scaleBitmapForDevice(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }/*from w  w  w. j a v a 2 s .c o m*/

    float density = Resources.getSystem().getDisplayMetrics().density;
    int newWidth = (int) (bitmap.getWidth() * density);
    int newHeight = (int) (bitmap.getHeight() * density);
    /*
    Bitmap resizeBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
     */
    /**
     * http://stackoverflow.com/questions/4821488/bad-image-quality-after-resizing-scaling-bitmap#7468636
     */
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2,
            new Paint(Paint.FILTER_BITMAP_FLAG));
    bitmap.recycle();

    return scaledBitmap;
}

From source file:Main.java

/**
 * Combines multiple bitmaps together into one
 * @param aParts An array of bitmaps/*from w  w w  .j  a  v a2  s. co  m*/
 * @return Returns a bitmap image
 */
public static Bitmap combineBitmaps(Bitmap[] aParts) {
    Bitmap[] parts = aParts;
    Bitmap result = Bitmap.createBitmap(parts[0].getWidth() * 2, parts[0].getHeight() * 2,
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    for (int i = 0; i < parts.length; i++) {
        canvas.drawBitmap(parts[i], parts[i].getWidth() * (i % 2), parts[i].getHeight() * (i / 2), paint);
    }
    return result;
}

From source file:Main.java

public static Bitmap rotateDrawable(Context context, @DrawableRes int resId, int angle) {
    Bitmap bmpOriginal = BitmapFactory.decodeResource(context.getResources(), resId);
    Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(),
            Bitmap.Config.ARGB_8888);//from  www.  j  a  va 2 s.c o m
    Canvas tempCanvas = new Canvas(bmResult);
    tempCanvas.rotate(angle - 90, bmpOriginal.getWidth() / 2, bmpOriginal.getHeight() / 2);
    tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
    return bmResult;
}

From source file:Main.java

public static byte[] bmp2bytesStream(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width % 8 != 0) {
        int adjustWidth = width + (8 - width % 8);
        final Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap whiteBgBitmap = Bitmap.createBitmap(adjustWidth, height, config);
        Canvas canvas = new Canvas(whiteBgBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, 0, 0, null);

        bitmap = whiteBgBitmap;/*from   ww w . j  a  va 2 s.c o  m*/
        width = bitmap.getWidth();
    }
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    byte[] image = new byte[width * height];
    final byte WHITE = 0, BLACK = 1;
    for (int i = 0; i < pixels.length; i++) {
        image[i] = (pixels[i] != 0xFFFFFFFF) ? BLACK : WHITE;
    }
    final int COL = width + width % 2;
    byte[] row = new byte[COL];
    byte[] res = new byte[COL / 8 * height];
    for (int i = 0, dex = 0, num = 0; i < height; ++i) {
        System.arraycopy(image, i * width, row, 0, width);
        for (byte e : row) {
            res[dex] += e << (7 - num++);
            num = 8 == num ? 0 : num;
            dex = 0 == num ? dex + 1 : dex;
        }
    }
    return res;
}

From source file:Main.java

/**
 * Retrieves a copy of the specified drawable resource, rotated by a specified angle.
 *
 * @param resources The current resources.
 * @param resourceId The resource ID of the drawable to rotate.
 * @param angle The angle of rotation./*from  w w w  . java2 s .  c o  m*/
 * @return Rotated drawable.
 */
public static Drawable getRotatedDrawable(android.content.res.Resources resources, int resourceId,
        float angle) {

    // Get the original drawable and make a copy which will be rotated.
    Bitmap original = BitmapFactory.decodeResource(resources, resourceId);
    Bitmap rotated = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);

    // Perform the rotation.
    Canvas tempCanvas = new Canvas(rotated);
    tempCanvas.rotate(angle, original.getWidth() / 2, original.getHeight() / 2);
    tempCanvas.drawBitmap(original, 0, 0, null);

    return new BitmapDrawable(resources, rotated);
}

From source file:Main.java

public static Bitmap rotateAndFrame(Bitmap bitmap) {
    final boolean positive = sRandom.nextFloat() >= 0.5f;
    final float angle = (ROTATION_ANGLE_MIN + sRandom.nextFloat() * ROTATION_ANGLE_EXTRA)
            * (positive ? 1.0f : -1.0f);
    final double radAngle = Math.toRadians(angle);

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    final double cosAngle = Math.abs(Math.cos(radAngle));
    final double sinAngle = Math.abs(Math.sin(radAngle));

    final int strokedWidth = (int) (bitmapWidth + 2 * PHOTO_BORDER_WIDTH);
    final int strokedHeight = (int) (bitmapHeight + 2 * PHOTO_BORDER_WIDTH);

    final int width = (int) (strokedHeight * sinAngle + strokedWidth * cosAngle);
    final int height = (int) (strokedWidth * sinAngle + strokedHeight * cosAngle);

    final float x = (width - bitmapWidth) / 2.0f;
    final float y = (height - bitmapHeight) / 2.0f;

    final Bitmap decored = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(decored);

    canvas.rotate(angle, width / 2.0f, height / 2.0f);
    canvas.drawBitmap(bitmap, x, y, sPaint);
    canvas.drawRect(x, y, x + bitmapWidth, y + bitmapHeight, sStrokePaint);

    return decored;
}

From source file:Main.java

@Deprecated
public static Bitmap getCombinedByPieces(List<Bitmap> bitmapList, int numStages) {
    //TODO add here the method to greyscale to use the same canvas but to draw a grayscale version
    //i mean, don't use greyscale, but add here all the functionalities to reuse the canvas
    int originalTotalWidth = bitmapList.get(0).getWidth() * numStages;
    Bitmap finalBitmap = Bitmap.createBitmap(originalTotalWidth, bitmapList.get(0).getHeight(),
            Bitmap.Config.ARGB_8888);/*  ww w  . j  a v  a2  s .co m*/
    float delta = 0f;
    Canvas comboImage = new Canvas(finalBitmap);
    for (int i = 0; i < numStages; i++) {
        comboImage.translate(delta, 0f);
        comboImage.drawBitmap(bitmapList.get(i), 0f, 0f, null);
        delta = originalTotalWidth / numStages;
    }
    return finalBitmap;
}

From source file:Main.java

public static Bitmap forceConfig565(Bitmap original) {
    Bitmap convertedBitmap = original;//from ww w  . java  2s . co m
    if (original.getConfig() != Bitmap.Config.RGB_565) {
        convertedBitmap = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(convertedBitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        canvas.drawBitmap(original, 0, 0, paint);

        if (convertedBitmap != original) {
            original.recycle();
        }
    }

    return convertedBitmap;
}

From source file:Main.java

/**
 * save the bitmap to local,add give a white bg color
 * @param bitmap//www  . j a v  a 2  s. com
 * @param path
 * @return
 */
public static boolean saveBitmapNoBgToSdCard(Bitmap bitmap, String path) {
    BufferedOutputStream bos = null;
    try {
        File file = new File(path);
        if (file.exists())
            file.delete();
        bos = new BufferedOutputStream(new FileOutputStream(file));

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int w_new = w;
        int h_new = h;
        Bitmap resultBitmap = Bitmap.createBitmap(w_new, h_new, Bitmap.Config.ARGB_8888);
        //            Paint paint = new Paint();
        //            paint.setColor(Color.WHITE);
        Canvas canvas = new Canvas(resultBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, new Rect(0, 0, w, h), new Rect(0, 0, w_new, h_new), null);
        resultBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        resultBitmap.recycle();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:Main.java

/** Creates and returns a new bitmap which is the same as the provided bitmap
 * but with horizontal or vertical padding (if necessary)
 * either side of the original bitmap/*from   w w w.  j a v a2s .com*/
 * so that the resulting bitmap is a square.
 * @param bitmap is the bitmap to pad.
 * @return the padded bitmap.*/
public static Bitmap padBitmap(Bitmap bitmap) {
    int paddingX;
    int paddingY;

    if (bitmap.getWidth() == bitmap.getHeight()) {
        paddingX = 0;
        paddingY = 0;
    } else if (bitmap.getWidth() > bitmap.getHeight()) {
        paddingX = 0;
        paddingY = bitmap.getWidth() - bitmap.getHeight();
    } else {
        paddingX = bitmap.getHeight() - bitmap.getWidth();
        paddingY = 0;
    }

    Bitmap paddedBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingX, bitmap.getHeight() + paddingY,
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(paddedBitmap);
    canvas.drawARGB(0xFF, 0xFF, 0xFF, 0xFF); // this represents white color
    canvas.drawBitmap(bitmap, paddingX / 2, paddingY / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

    return paddedBitmap;
}