Paint a nine patch in a bitmap - java

I want to paint a Nine Patch into a Bitmap (filling all the space with the fill space). Thats my code but doesn't work. Can you help me?
Bitmap bmp= Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
Drawable drawable= getResources().getDrawable(R.drawable.car);
Canvas canvas= new Canvas(bmp);
drawable.draw(canvas);
iv2.setImageBitmap(bmp);

Try setting the bounds of your drawable before drawing:
Bitmap bmp = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
Drawable drawable = getResources().getDrawable(R.drawable.car);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
iv2.setImageBitmap(bmp);
Although given that it looks like you're just using an ImageView, I'm not sure why you're not just setting it directly using setImageDrawable.

Related

Porterduff Modes(Multiply)

I wanted to use porterduff multiplication mode to the PNG image, but the background became Black, only the background in the PNG extension picture becomes Black, how can I fix this?
The background should be transparent when I use the multiplication mode I want.
This line of code did not work:
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
I myself wrote code as below but it didn't work and didn't show any pictures.
private Bitmap MultiplyBitmap(Bitmap bitmapmultiply){
Bitmap bitmap = Bitmap.createBitmap(bitmapmultiply.getWidth(), bitmapmultiply.getHeight(), Bitmap.Config.ARGB_8888);
Canvas cnvs = new Canvas(bitmap);
Paint pnt = new Paint();
pnt.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
cnvs.drawBitmap(bitmapmultiply, 0, 0, pnt);
Bitmap multiplybitmap = Bitmap.createBitmap(bitmapmultiply.getWidth(), bitmapmultiply.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(multiplybitmap);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmapmultiply,0,0, null);
canvas.drawBitmap(bitmap, 0, 0, paint);
return multiplybitmap;
}
How can I remove the black background using Java? Thanks
Please, refer to the that PoterDuff.Mode link from Android webstie where you can find a lot of examples.
You could try something like this (just adapt the code for your needs):
Kotlin
val bmpDrawable = bmp.toDrawable(res)
bmpDrawable = DrawableCompat.setTint(bmpDrawable, Color.BLACK)
val tintedBmp = bmpDrawable.bitmap
return jokeDao.insert(entity.toDbJoke())
Java
Drawable bmpDrawable = bmp.toDrawable(res)
DrawableCompat.setTint(bmpDrawable, Color.BLACK)
Bitmap tintedBmp = bmpDrawable.bitmap

Error writing text on image

I try to write a text on my image:
private Bitmap drawText() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.test);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(5);
canvas.drawText("Some Text here", 5, 5, paint);
Bitmap resBitmap = Bitmap.createBitmap(canvas.getWidth(),
canvas.getHeight(), Config.RGB_565);
canvas.setBitmap(resBitmap);
return resBitmap;
}
the result is a completely black image. What am I doing wrong ?
Disclaimer: I'm not an Android developer. I've never written code like this. It's just my interpretation of the documentation...
I suspect you actually want to set the bitmap to draw onto much earlier, and then draw the other bitmap into the canvas. So something like:
private Bitmap drawText() {
// Load the existing image to get some dimensions
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.test);
// Create a result bitmap and a canvas which draws onto it
Bitmap resBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.RGB_565);
Canvas canvas = new Canvas(resBitmap);
// Draw the existing image into the canvas
canvas.drawBitmap(bitmap, 0f, 0f, null);
// Draw text on top
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(5);
canvas.drawText("Some Text here", 5, 5, paint);
return resBitmap;
}
u set background of canvas from this
paint.setColor(Color.BLACK);
and by default text color is also black i.e u can see only that black screen
try from this
paint.setColor(Color.GREEN);
change the Background color and foreground color of the canvas.
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawPaint(paint);
paint.setColor(android.R.color.black);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);
ensure that the color of the text must be different from the background color

Overlay images in Android

I have two images that I want to merge into one. (Eg "House.png" on top of "street.png")
How do i achieve this in Android? I just want to merge the images and export them to a file.
This example Sets the image to an ImageView but i wish to export it.
This other example does not work in Android since the classes are not available.
I'd try something like:
public static Bitmap mergeImages(Bitmap bottomImage, Bitmap topImage) {
final Bitmap output = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage
.getHeight(), Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawBitmap(bottomImage, 0, 0, paint);
canvas.drawBitmap(topImage, 0, 0, paint);
return output;
}
(not tested, I just wrote it here, might be some simple errors in there)
Basically what you do is create a 3rd empty bitmap, draw the bottom image on it and then draw the top image over it.
As for saving to a file, here are a few examples: Save bitmap to location
You can do like this...............
public Bitmap Overlay(Bitmap Bitmap1, Resources paramResources, Bitmap Bitmap2, int alpha)
{
Bitmap bmp1 = Bitmap.createScaledBitmap(Bitmap2, Bitmap1.getWidth(), Bitmap1.getHeight(), true);
Bitmap bmp2 = Bitmap.createBitmap(Bitmap1.getWidth(), Bitmap1.getHeight(), Bitmap1.getConfig());
Paint localPaint = new Paint();
localPaint.setAlpha(alpha);
Canvas localCanvas = new Canvas(bmp2);
Matrix localMatrix = new Matrix();
localCanvas.drawBitmap(Bitmap1, localMatrix, null);
localCanvas.drawBitmap(bmp1, localMatrix, localPaint);
bmp1.recycle();
System.gc();
return bmp2;
}

Canvas as ImageView/Bitmap

I cant get my head around this problem I have. I have 2 images which were added to a canvas and will be treated as one object, now I need to return this canvas as bitmap/drawable, since
Here is code how I added 2 bitmaps into a canvas
Bitmap image1=BitmapFactory.decodeResource(getResources() ,R.drawable.icon1);
Bitmap image2=BitmapFactory.decodeResource(getResources() R.drawable.icon2);
Rect srcRect = new Rect(0, 0, image.getWidth(), image.getHeight());
Rect dstRect = new Rect(srcRect);
dstRect.offset(15, 0);
canvas.drawBitmap(image, srcRect, dstRect, null);
dstRect.offset(image.getWidth(), 0);
canvas.drawBitmap(image2, srcRect, dstRect, null);
//return???????????
Please someone help.
Tnx in advance!
You can create a Bitmap to draw into.
Bitmap image1=BitmapFactory.decodeResource(getResources() ,R.drawable.icon1);
Bitmap image2=BitmapFactory.decodeResource(getResources() R.drawable.icon2);
Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);//Create the canvas to your image
Rect srcRect = new Rect(0, 0, image.getWidth(), image.getHeight());
Rect dstRect = new Rect(srcRect);
dstRect.offset(15, 0);
canvas.drawBitmap(image, srcRect, dstRect, null); //draw on it
dstRect.offset(image.getWidth(), 0);
canvas.drawBitmap(image2, srcRect, dstRect, null);
return result;//result will have the drawed images from the canvas
Where did you get the canvas from? If from a bitmap, then that obj will now have whatever you drew on the canvas applied to it.
Canvas is just a way to draw onto a bitmap or drawable that it is backed by. So if you create a Bitmap called result and then get your canvas from that you can just return that.
As in..
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
...do your stuff...
return result;

Android Edit Bitmap Channels

It's possible to access the alpha channel of a given bitmap with extractAlpha(), but I haven't been able to find any way to actually set the alpha channel of a bitmap.
How can multiple greyscale images be recombined as channels into a Bitmap with Android?
It is quite possible to re-combine separate channels back into an ARGB image. You just need the grayscale channel images and an image with the alpha channel you want - note that this is not an opaque grayscale image, but an image with the alpha you want. You then draw each channel with a Paint using the appropriate PorterDuffXfermode onto a blank, black-filled Bitmap.
// have your 3 channel grayscales and 1 alpha bitmap loaded by this point
Paint redPaint = new Paint();
redPaint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
redPaint.setShader(new BitmapShader(redChanImg, TileMode.CLAMP, TileMode.CLAMP));
redPaint.setColorFilter(new PorterDuffColorFilter(Color.RED, Mode.DARKEN));
Paint greenPaint = new Paint();
greenPaint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
greenPaint.setShader(new BitmapShader(greenChanImg, TileMode.CLAMP, TileMode.CLAMP));
greenPaint.setColorFilter(new PorterDuffColorFilter(Color.GREEN, Mode.DARKEN));
Paint bluePaint = new Paint();
bluePaint.setXfermode(new PorterDuffXfermode(Mode.LIGHTEN));
bluePaint.setShader(new BitmapShader(blueChanImg, TileMode.CLAMP, TileMode.CLAMP));
bluePaint.setColorFilter(new PorterDuffColorFilter(Color.BLUE, Mode.DARKEN));
Paint alphaPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
alphaPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
c.setBitmap(resultImage);
c.drawRect(0, 0, width, height, redPaint);
c.drawRect(0, 0, width, height, greenPaint);
c.drawRect(0, 0, width, height, bluePaint);
c.drawBitmap(alphaImg, 0, 0, alphaPaint);
//save off resultImage, display it, etc...
With the above code and the following 4 images (red, green, blue, and alpha, respectively):
We get the following result:
Just a quick note: the red oval is an opaque, red oval on a transparent background - the color doesn't matter for this one, but the alpha does
Manipulating Bitmaps is a farily simple thing, when to access the pixel (bytes) directly.
To do that in Android you can do it over this approch
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] b = bos.toByteArray();
Now you can do any image manipulation, tranformation or combination you like.
I hope this is what you were looking for.
Have you tried with canvas? The following looks like a hack, but maybe it will work. I have not tested it myself.
Bitmap bitmap;
int color = bitmap.getPixel(1, 123);
Rect rect = new Rect(1,123,2,124);
Canvas c = new Canvas(bitmap);
c.clipRect(rect);
c.drawARGB(50, Color.red(color), Color.green(color), Color.blue(color));

Categories

Resources