Is possible convert BitmapDescriptor to Bitmap? - java

I need to convert BitmapDescriptor to Bitmap.
I can convert a bitmap to BitmapDescriptor with this code:
BitmapDescriptor bd = BitmapDescriptorFactory.fromBitmap(bitmap);
but how can i do the inverse way ?
I need to get BitmapDescriptorFactory.defaultMarker() in bitmap format.

I m not sure but I don't think so it is possible
In my application i have requirement to change default market color ...i got the default marker as bitmap using below code hope it helps u
Drawable d = DrawableUtils.resizeImageToDrawable(
MapViewFragment.mapViewActivity,
Configuration.Display.getDrawableFix(i),
Configuration.MapView.getWaypointIconWidth(),
Configuration.MapView.getWaypointIconHeight());
d.setColorFilter(color, Mode.MULTIPLY);
Bitmap b = ((BitmapDrawable) d).getBitmap();
Canvas myCanvas = new Canvas(b);
int myColor = b.getPixel(0,0);
ColorFilter filter = new LightingColorFilter(myColor, color);
If any questions u can ask

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.

How to generate a 24bit Bitmap in android

I am working on android app that needs to generate a QRCode, and I successfully done with this.
My problem is on the printing side. I used a Mobiprint 3 device which has a built-in Thermal Printer. But my problem is the device printer only support a 24bit Bitmap.
My question is, is there a way to create a 24bit Bitmap in android? since it only support a 32bit. I googled it in a week but no one solved my problem.
Thank you in advance.
BTW.
This is my code
//method for generating a QRCode Bitmap
try {
bitmap = qrGenerator.generateQRCode(duCode);
int width, height;
height = bitmap.getHeight();
width = bitmap.getWidth();
bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmap, 0, 0, paint);
} catch (WriterException e) {
e.printStackTrace();
}
}
and this is when calling the Bitmap for printing.
print.printBitmap(getBitmap());
this codes only prints a pure square black.
PS: print.printBitmap() is from the Mobiprint API.

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

Decode specific areas of image in Bitmapfactory?

I'm working with GeoTiff/PNG files too large for handling as a whole in my code.
Is there any possibility to decode specific areas (e.g. given by two x,y coordinates) of a file in bitmapfactory? Haven't found anything looking similar at http://developer.android.com/reference/android/graphics/BitmapFactory.html(Android's developer reference).
Thanks!
With kcoppock's hint I've set up the following solution.
Though I'm wondering why rect needs to be initialized by Rect(left, bottom, right, top) instead of Rect(left, top, right, bottom)...
Example call:
Bitmap myBitmap = loadBitmapRegion(context, R.drawable.heightmap,
0.08f, 0.32f, 0.13f, 0.27f);
Function:
public static Bitmap loadBitmapRegion(
Context context, int resourceID,
float regionLeft, float regionTop,
float regionRight, float regionBottom) {
// Get input stream for resource
InputStream is = context.getResources().openRawResource(resourceID);
// Set options
BitmapFactory.Options opt = new BitmapFactory.Options();
//opt.inPreferredConfig = Bitmap.Config.ARGB_8888; //standard
// Create decoder
BitmapRegionDecoder decoder = null;
try {
decoder = BitmapRegionDecoder.newInstance(is, false);
} catch (IOException e) {
e.printStackTrace();
}
// Get resource dimensions
int h = decoder.getHeight();
int w = decoder.getWidth();
// Set region to decode
Rect region = new Rect(
Math.round(regionLeft*w), Math.round(regionBottom*h),
Math.round(regionRight*w), Math.round(regionTop*h));
// Return bitmap
return decoder.decodeRegion(region, opt);
}
You should look into BitmapRegionDecoder. It seems to describe exactly the use case that you are looking for.
I don't know exactly what you mean by "Decode specific areas" but if by decoding you mean, to actually "copy" certain areas of a bitmap, what you can do is make use of canvas in order to get it as shown below:
Bitmap bmpWithArea = Bitmap.createBitmap(widthDesired, heightDesired, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpWithArea);
Rect area = new Rect(arealeft, areatop, arearight, areabottom);
Rect actualSize = new Rect(0, 0, widthDesired, heightDesired);
canvas.drawBitmap(bitmapWithAreaYouWantToGet, area, actual, paintIfAny);
//And done, starting from this line "bmpWithArea" has the bmp that you wanted, you can assign it to ImageView and use it as regular bmp...
Hope this helps...
Regards!

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

Categories

Resources