I am trying to add an image watermark to another image. I have the following code but I'm facing a problem. I don't know what this 'Resources res' is.
Can anyone help?
public static Bitmap addWatermark(Resources res, Bitmap source)
{
int w, h;
Canvas c;
Paint paint;
Bitmap bmp, watermark;
Matrix matrix;
float scale;
RectF r;
w = source.getWidth();
h = source.getHeight();
bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG |Paint.FILTER_BITMAP_FLAG);
c = new Canvas(bmp);
c.drawBitmap(source, 0, 0, paint);
watermark = BitmapFactory.decodeResource(res, R.drawable.android_mo);
scale = (float) (((float) h * 0.10) / (float) watermark.getHeight());
matrix = new Matrix();
matrix.postScale(scale, scale);
r = new RectF(0, 0, watermark.getWidth(), watermark.getHeight());
matrix.mapRect(r);
matrix.postTranslate(w - r.width(), h - r.height());
c.drawBitmap(watermark, matrix, paint);
watermark.recycle();
return bmp;
}
It's the Resource object you can have through activity.getResources() or fragment.getResources()
Resources res
is the resource object that you need to pass from the calling Activity/Fragment.
suppose, if the method is in Utility class then invoke the method as
//Add watermark to the selected image
Bitmap markedBitmap = Utility.addWatermark(context.getResources(), bitmap);
Main bitmap is bmp1 and bmp2 transparante bitmap :
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;
}
Related
I'm trying to draw a bitmap on the top of another bitmap like this :
I'm using the following code to create an empty background with 420x420 as size, and draw the star on it :
for (int i = 0; i < 10; i++) {
Bitmap resized;
if (stretchedPosition.contains(i)) {
resizedStar = Bitmap.createScaledBitmap(star, star.getWidth() - 80, star.getHeight() + 80, true);
} else
resizedStar = Bitmap.createScaledBitmap(star, star.getWidth() + 80, star.getHeight() - 80, true);
resized = makeBackground(resized);
//code for generating a GIF from bitmaps
}
public Bitmap makeBackground(Bitmap resized) {
Bitmap emptyBitmap = Bitmap.createBitmap(420, 420, Bitmap.Config.ARGB_8888);
int positionLeft = 0;
int positionTop = 0;
Bitmap newBitmap = Bitmap.createBitmap(emptyBitmap.getWidth(), emptyBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(emptyBitmap, positionLeft, positionTop, null);
canvas.drawColor(Color.GREEN);
int bitmap1Width = resized.getWidth();
int bitmap1Height = resized.getHeight();
int bitmap2Width = emptyBitmap.getWidth();
int bitmap2Height = emptyBitmap.getHeight();
float marginLeft = (float) (bitmap1Width * 0.5 - bitmap2Width * 0.5);
float marginTop = (float) (bitmap1Height * 0.5 - bitmap2Height * 0.5);
canvas.drawBitmap(resized, new Matrix(), null);
canvas.drawBitmap(emptyBitmap, marginLeft, marginTop, null);
return newBitmap;
}
THE ISSUE:
As you can see here, the girl image is not centered and the image gets cut as well.
This may helps you;
Bitmap fileBitmap = BitmapFactory.decodeFile(filePath);
Bitmap blankBitmap = getBlankBitmap(YOUR_WIDTH, YOUR_HEIGHT);
Bitmap mergeBitmap = overlay(blankBitmap, fileBitmap);
you can generate blank bitmap using following method:
public static Bitmap getBlankBitmap(int w, int h) {
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
Canvas canvas = new Canvas(bmp);
return bmp;
}
and Merge two bitmap like this;
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
int padding = (bmp1.getWidth() / 2) - (bmp2.getWidth() / 2);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, padding, 0, null);
bmp1.recycle();
bmp2.recycle();
return bmOverlay;
}
I have an image, and I am trying to overlay a cropped version of the image over the original. Something like this:
Original
Overlay Image
Image after the overlay image is cropped to roughly 50%
Right now, my code below reverses what I want, and returns an image like this:
public void setOverlay(ImageView image, Bitmap originalBitmap, double percentageCompleted) {
int percentHeight;
int height = originalBitmap.getHeight();
Bitmap cropped;
if(percentageCompleted == 0){
cropped = Bitmap.createBitmap(overlay, 0, 0, overlay.getWidth() , 0 );
} else {
percentHeight = (int) Math.floor(height * (percentageCompleted));
Log.d("HEIGHT", Double.toString(height));
Log.d("PERCENT Completed", Double.toString(percentageCompleted));
Log.d("PERCENT HEIGHT", Integer.toString(percentHeight));
cropped = Bitmap.createBitmap(overlay, 0, 0, overlay.getWidth() , height-percentHeight);
}
originalBitmap = overlay(originalBitmap, cropped);
//set imageview to new bitmap
image.setImageBitmap(originalBitmap );
}
public Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmp3 = bmp1.copy(Bitmap.Config.ARGB_8888,true);//mutable copy
Canvas canvas = new Canvas(bmp3 );
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmp3;
}
I know I could reverse the image files, but I want to start with the original image and have the overlay draw upwards, rather than starting with the overlay, and drawing the original downwards. Any help is appreciated!
You can use this method:
Canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
Here is my overlay method that may meet your need:
public static Bitmap overlay(Bitmap base, Bitmap overlay, Float percentage) {
Bitmap resultBitmap = Bitmap.createBitmap(base.getWidth(), base.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(resultBitmap);
Paint paint = new Paint();
// base bitmap
canvas.drawBitmap(base, 0F, 0F, paint);
// overlay bitmap
int yOffset = (int) (percentage * base.getHeight());
Rect rect = new Rect(0, yOffset, overlay.getWidth(), overlay.getHeight());
canvas.drawBitmap(overlay, rect, rect, paint);
return resultBitmap;
}
I am trying to draw something like this, but it is not displaying bitmap 2. Only bitmap 1 (the larger one) displays.
//bitmap1 and bitmap2 already initialized
Bitmap resultBitmap = Bitmap.createBitmap(cubemap.getWidth(), cubemap.getHeight(), cubemap.getConfig());
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(bitmap1, new Matrix(), null);
canvas.drawBitmap(bitmap2, (bitmap1.getWidth() - bitmap2.getWidth()) / 3, (bitmap1.getHeight()) , new Paint());
return resultBitmap;
set image in Imageview
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setImageBitmap(drawUsingBitmap());
Using Bitmap
public Bitmap drawUsingBitmap() {
bitmapLarge = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap_large);
bitmapSmall = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap_small);
Bitmap resultBitmap = Bitmap.createBitmap(bitmapLarge.getWidth(), bitmapSmall.getHeight() + bitmapLarge.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(resultBitmap);
// using bitmap
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawBitmap(bitmapSmall, bitmapSmall.getWidth() / 2, 0, paint);
canvas.drawBitmap(bitmapLarge, 0, bitmapSmall.getHeight() - 10, paint);
return resultBitmap;
}
Using drawRect
public Bitmap draWUsingRect() {
// using rectangle
int widthSmall = 120;
int heightSmall = 80;
int widthLarge = 400;
int heightLarge = 300;
Bitmap resultBitmap = Bitmap.createBitmap(widthLarge + 20, heightSmall + heightLarge + 20, Config.ARGB_8888);
Canvas canvas = new Canvas(resultBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5);
canvas.drawRect(new Rect(widthSmall / 2, 0, widthSmall, heightSmall), paint);
canvas.drawRect(new Rect(0, heightSmall, widthLarge, heightSmall + heightLarge), paint);
return resultBitmap;
}
I hope it is helpful to you.
I want to merge two bitmaps side-by-side into one bitmap. The following code is merge sub-bottom. How do I merge side-by-side into one bitmap ?
public Bitmap mergeBitmap(Bitmap fr, Bitmap sc)
{
Bitmap comboBitmap;
int width, height;
width = fr.getWidth() + sc.getWidth();
height = fr.getHeight();
comboBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(comboBitmap);
comboImage.drawBitmap(fr, 0f, 0f, null);
comboImage.drawBitmap(sc, 0f , fr.getHeight(), null);
return comboBitmap;
}
public Bitmap mergeBitmap(Bitmap fr, Bitmap sc)
{
Bitmap comboBitmap;
int width, height;
width = fr.getWidth() + sc.getWidth();
height = fr.getHeight();
comboBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(comboBitmap);
comboImage.drawBitmap(fr, 0f, 0f, null);
comboImage.drawBitmap(sc, fr.getWidth(), 0f , null);
return comboBitmap;
}
This article goes through the process of combining 2 images one below the other(only works with PNG or JPG ). It will involve passing 2 Bitmaps, which will then get combined using the Canvas class. You can do somme minor changes to get your two images side by side:
public Bitmap combineImages(Bitmap c, Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom
Bitmap cs = null;
int width, height = 0;
if(c.getHeight() > s.getHeight()) {
width = c.getWidth() + s.getWidth(;
height = c.getHeight());
} else {
width = c.getWidth() + s.getWidth();
height = s.getHeight();
}
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(c, 0f, 0f, null);
comboImage.drawBitmap(s, c.getWidth(), 0f, null);
// this is an extra bit I added, just incase you want to save the new image somewhere and then return the location
/*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
OutputStream os = null;
try {
os = new FileOutputStream(loc + tmpImg);
cs.compress(CompressFormat.PNG, 100, os);
} catch(IOException e) {
Log.e("combineImages", "problem combining images", e);
}*/
return cs;
}
How to set all white the 10 rows on the left side of a Bitmap?
I'v got a Bitmap that has to be padded on the left side. I thought i can create a new image iterate on the old one getpixel for each position and setpixel on the new one (white or colored) than return the new bitmap...is this wrong?
Any suggestion? thanks a lot!
You can instead create a new Bitmap with the extra padding number of pixels.
Set this as the canvas bitmap and Color the entire image with the required color and then copy your bitmap.
public Bitmap pad(Bitmap Src, int padding_x, int padding_y) {
Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
Canvas can = new Canvas(outputimage);
can.drawARGB(FF,FF,FF,FF); //This represents White color
can.drawBitmap(Src, padding_x, padding_y, null);
return outputimage;
}
public Bitmap addPaddingTopForBitmap(Bitmap bitmap, int paddingTop) {
Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingTop, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
canvas.drawColor(Color.RED);
canvas.drawBitmap(bitmap, 0, paddingTop, null);
return outputBitmap;
}
public Bitmap addPaddingBottomForBitmap(Bitmap bitmap, int paddingBottom) {
Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight() + paddingBottom, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
canvas.drawColor(Color.RED);
canvas.drawBitmap(bitmap, 0, 0, null);
return outputBitmap;
}
public Bitmap addPaddingRightForBitmap(Bitmap bitmap, int paddingRight) {
Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingRight, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
canvas.drawColor(Color.RED);
canvas.drawBitmap(bitmap, 0, 0, null);
return outputBitmap;
}
public Bitmap addPaddingLeftForBitmap(Bitmap bitmap, int paddingLeft) {
Bitmap outputBitmap = Bitmap.createBitmap(bitmap.getWidth() + paddingLeft, bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
canvas.drawColor(Color.RED);
canvas.drawBitmap(bitmap, paddingLeft, 0, null);
return outputBitmap;
}
Here's a kotlin extension function with RxJava to get it done. I haven't tested completely but based combined the previous answers to get something
fun Bitmap.pad(top: Float = 0F, bottom: Float = 0F, left: Float = 0F, right: Float = 0F): Single<Bitmap> {
return Single.create<Bitmap> { emitter ->
val output = Bitmap.createBitmap(
(width + left + right).toInt(),
(height + top + bottom).toInt(),
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(output)
canvas.drawBitmap(this, left, top, null)
emitter.onSuccess(output)
}.subscribeOn(Schedulers.computation())
}
I think the coroutine version would simply be
suspend fun Bitmap.pad(top: Float = 0F, bottom: Float = 0F, left: Float = 0F, right: Float = 0F): Bitmap {
val output = Bitmap.createBitmap(
(width + left + right).toInt(),
(height + top + bottom).toInt(),
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(output)
canvas.drawBitmap(this, left, top, null)
return output
}
I just did this to give padding from all side. Hope it will help someone.
Combination of https://stackoverflow.com/a/44060669/6480433 & https://stackoverflow.com/a/6957333/6480433 these answer.
Bitmap outputimage = Bitmap.createBitmap(Src.getWidth() + padding_x,Src.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
Canvas can = new Canvas(outputimage);
can.drawBitmap(Src, padding_x, padding_y, null);
Bitmap output = Bitmap.createBitmap(outputimage.getWidth()+padding_x, outputimage.getHeight() + padding_y, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
canvas.drawBitmap(outputimage, 0, 0, null);
return output;
You might want to look here:
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html
methods you might want to use are: getHeight() then you know how many pixels to set and iterate over 10 columns
and setRGB (int x, int y, int RGB) to set the pixel