How to add small bitmap in front of large bitmap image - java

we need to merge two images with background large image and in front of that a small bitmap and I am using this code then the result is like this what is wrong and how to solve this problem. here the image of the small image is not visible but in small image place a large image is placed
**
Java code
**
Bitmap backgroundimage = BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.drawable.rec_);
BitmapDrawable mBitmapDrawable= new BitmapDrawable( overlay(bitmap,backgroundimage));
mBitmapDrawable.setGravity(CENTER);
toolbar_layout.setBackground(mBitmapDrawable);
private 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, new Matrix(), null);
return bmOverlay;
}

Related

Convert OpenCV mat to Android Bitmap

I am trying to convert an OpenCV mat to android Bitmap but that gives me images with a bluish tint(yellow colour turns to blue)! Even though I am not doing any processing on the image! I have no clue why this is happening. Below is some relevant code:
File file = new File(imgDecodableString);
image = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_COLOR);
resultBitmap = Bitmap.createBitmap(image.cols(), image.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(image, resultBitmap);
Bitmap mResult = resultBitmap;
ImageView imgView = (ImageView) findViewById(R.id.imgView);
imgView.setImageBitmap(mResult);
//imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
I am new to android app development so I might be missing something simple. Thanks for help!
EDIT:
I am uploading images for reference.
Based on the given suggestions, I made a function which converts Mat to Bitmap. This function works perfectly.!
private static Bitmap convertMatToBitMap(Mat input){
Bitmap bmp = null;
Mat rgb = new Mat();
Imgproc.cvtColor(input, rgb, Imgproc.COLOR_BGR2RGB);
try {
bmp = Bitmap.createBitmap(rgb.cols(), rgb.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgb, bmp);
}
catch (CvException e){
Log.d("Exception",e.getMessage());
}
return bmp;
}
As suspected, the issue is with the RGB color convention, Android follows RGB color convention, but OpenCV follows BGR color convention, You can rectify it using Imgproc.cvtColor(mat, Imgproc.COLOR_BGR2RGBA), before displaying it in the ImageView.

Get Bitmap from ImageView in Android L

I want to get Bitmap from ImageView. I have used following code, but getDrawable() returns null. How to get whole Bitmap from ImageView.
Bitmap bitmap;
if (mImageViewer.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageViewer.getDrawable()).getBitmap();
} else {
Drawable d = mImageViewer.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
storeImage(bitmap,"final.jpeg");
If you just want the Bitmap from a ImageView the following code may work for you:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)
Could be that the emulator or device your using has a different density and is trying to pull images from another folder.
If you are using an extension in your image other than .png, .jpg, or .gif, It might not recognize other extension types. http://developer.android.com/guide/topics/resources/drawable-resource.html
According to this answer, just do it like this:
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
For Kotlin:
simply write this code to get the bitmap from an ImageView
imageview.invalidate()
val drawable = imageview.drawable
val bitmap = drawable.toBitmap()
If you are trying to get bitmap from Glide loaded image then this will help you
Drawable dr = ((ImageView) imView).getDrawable();
Bitmap bmp = ((GlideBitmapDrawable)dr.getCurrent()).getBitmap();
Take a picture of the ImagView and convert it to a string to send to the server
ImageView ivImage1 = (ImageView ) findViewById(R.id.img_add1_send );
getStringImage( ( ( BitmapDrawable ) ivImage1.getDrawable( ) ).getBitmap( ) ),
public String getStringImage(Bitmap bm){
ByteArrayOutputStream ba=new ByteArrayOutputStream( );
bm.compress( Bitmap.CompressFormat.PNG,90,ba );
byte[] by=ba.toByteArray();
String encod= Base64.encodeToString( by,Base64.DEFAULT );
return encod;
}

Paint a nine patch in a bitmap

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.

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;
}

Android mapView: rotate overlay item

I want to rotate an overlay item (a drawable) according to my bearing onLocationChanged. How do I do this? I tried with the drawable, with the itemizedOverlay, and with the overlayItem, but no success.
public void bearingRotation(float boatBearing){
Bitmap bm = ((BitmapDrawable)drawableCurrPos).getBitmap();
Matrix mat = new Matrix();
mat.postRotate(boatBearing);
Bitmap bMapRotate = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), mat, true);
drawableCurrPos = new BitmapDrawable(bMapRotate);
}
I would suggest that you convert that drawable to bitmap.
Bitmap bm = ((BitmapDrawable)drawable).getBitmap();
Matrix mat = new Matrix();
mat.postRotate(90);
Bitmap bMapRotate = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), mat, true);
Convert this bMapRotate using BitmapDrawable and apply that as a pin pointer to your location.

Categories

Resources