I need to figure out a way to get a bitmap cropped without creating another bitmap that is already present.
I have two bitmaps
int width = 50;
int height = 50;
//Bitmap A
bitmapA = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmapA.copyPixelsFromBuffer(buffer);
//Bitmap B
bitmapB = Bitmap.createBitmap(45, 45, Bitmap.Config.ARGB_8888);
bitmapA has an image data, while bitmapB has no image data. I need a cropped image of bitmapA starting from (5, 5) and put it into bitmapB without creating another new Bitmap.
While I can do
bitmapC = Bitmap.createBitmap(bitmapA, 5, 5, 45, 45);
this will create another Bitmap object. I need to copy the data of bitmapA into the already created bitmapB without creating another Bitmap / calling createBitmap
Can this be done by using Rect?
Any ideas? thanks.
Related
I am working on an Android app, And I put many pictures in Assets directory by categories. And I get a bitmap of a image using code below
InputStream in = getAssets().open(file);
Bitmap bm = BitmapFactory.decodeStream(in);
Bitmap board = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(board);
canvas.drawBitmap(bm,0,0,null);
And the width and height(pixel) of all my image files is 360x240, I want the Bitmap:board to be 960x720 when the images are read because I need convert the bitmap to a 960x720 Mat using OpenCV.
What can I do?
You can use createScaledBitmap :
Bitmap createScaledBitmap (Bitmap src,
int dstWidth,
int dstHeight,
boolean filter)
Creates a new bitmap, scaled from an existing bitmap, when possible.
If the specified width and height are the same as the current width
and height of the source bitmap, the source bitmap is returned and no
new bitmap is created.
I want to save the canvas object in onDraw() method to be saved as a bitmap. Please do not suggest answers like "view.getDrawingCache(true)" .I want to save canvas directly to a bitmap
// first create a mutable bitmap - you determine the size
bkg = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
// create a canvas with that empty bitmap
Canvas c = new Canvas(bkg);
// do whatever drawing methoods you need....I did a circle
c.drawColor(mContext.getResources().getColor(R.color.off_white));
p.setColor(pixel);
c.drawCircle(width / 2, width / 2, width / 2, p);
// then pull off the entire canvas as a bitmapdrawable (or bitmap, if you perfer)
return new BitmapDrawable(mContext.getResources(), bkg);
I have to crop a bitmap image. For this, I am using
Bitmap bitmap = Bitmap.createBitmap(imgView.getWidth(),imgView.getHeight(), Bitmap.Config.RGB_565);
Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100);
bitmap.recycle();
Canvas canvas = new Canvas(result);
imgView.draw(canvas);
But it cuts the bottom and right of the bitmap. Top and Left part of the bitmap exists in the output. That means x and y position has no effect.
I am searched for good documentation. But I couldn't.
Thanks in Advance
What is the problem here and how to solve?
Basically your problem arises form the fact that you create a bitmap. You don't put anything in it. You then create a smaller bitmap and then you render an imageView to that smaller bitmap.
This cuts off the bottom 100 pixels and right 20 pixels.
You need to Create the large bitmap. Add the imageview data to that bitmap. Then resize it.
The following code should work:
Bitmap bitmap = Bitmap.createBitmap(imgView.getWidth(),imgView.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
imgView.draw(canvas);
Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100);
bitmap.recycle();
I wanted to create an app by taking images from the sd card and add caption to it. Also I wanted to move the caption as per our wish and place it somewhere as desired and save it .can you please suggest an idea for doing this. Also if we want to add a caption to the existing image from SD card then , do we need to have database for doing this. or can it be saved directly to the SD card.
Refer This Answer
You can put an EditText and Write into it and after writting you first convert it to Bitmap like
Bitmap bmp = Bitmap.createBitmap(mEditText.getDrawingCache());
Now you can add created image bmp to your original image like this
call :
Bitmap combined = combineImages(bgBitmap,bmp);
public Bitmap combineImages(Bitmap background, Bitmap foreground) {
int width = 0, height = 0;
Bitmap cs;
width = getWindowManager().getDefaultDisplay().getWidth();
height = getWindowManager().getDefaultDisplay().getHeight();
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
background = Bitmap.createScaledBitmap(background, width, height, true);
comboImage.drawBitmap(background, 0, 0, null);
comboImage.drawBitmap(foreground, matrix, null);
return cs;
}
Use canvas (Custom Views) for it..
I think this is the only way to make one view (Image in one canvas) on other view (caption in other canvas) on it. So in this you have to complete knowledge of how views draw on canvas and how to move to that views. And using canvas you can also smoothly move your views on screen.
After creating custom views you can save those views in Bitmap, now you can also combine those images to one bitmap..
(Here Customeviews are ImageViews, TextViews, EditText etc..)
All the best. :-)
USe like this.
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
background = Bitmap.createScaledBitmap(background, width, height, true);
comboImage.drawBitmap(background, 0, 0, null);
comboImage.drawText("title", x, y, paint);
I want to merge two images and then save them on the Android SDCard.One is from the camera and one from the resources folder. The problem is that i get this error: Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor. Thanks.
Bitmap bottomImage = BitmapFactory.decodeResource(getResources(),R.drawable.blink);
Bitmap topImage = (Bitmap) data.getExtras().get("data");
// As described by Steve Pomeroy in a previous comment,
// use the canvas to combine them.
// Start with the first in the constructor..
Canvas comboImage = new Canvas(bottomImage);
// Then draw the second on top of that
comboImage.drawBitmap(topImage, 0f, 0f, null);
// bottomImage is now a composite of the two.
// To write the file out to the SDCard:
OutputStream os = null;
try {
os = new FileOutputStream("/sdcard/DCIM/Camera/" + "myNewFileName.png");
bottomImage.compress(CompressFormat.PNG, 50, os);
//Bitmap image.compress(CompressFormat.PNG, 50, os);
} catch(IOException e) {
Log.v("error saving","error saving");
e.printStackTrace();
}
Managed to fix it by simply doing this change:
int w = bottomImage.getWidth();
int h = bottomImage.getHeight();
Bitmap new_image = Bitmap.createBitmap(w, h ,bottomImage.getConfig());
The problem now is that it doesn't saves the image. Do you know why?
This will help you =)
Edit: (embed answer from link)
the only static "constructor" for Bitmap returning a mutable one is:
(Class: Bitmap) public static Bitmap createBitmap(int width, int
height, boolean hasAlpha)
Returns: a mutable bitmap with the specified width and height.
So you could work with getPixels/setPixels or like this:
Bitmap bitmapResult = bm.createBitmap(widthOfOld, heightOfOld, hasAlpha);
Canvas c = new Canvas();
c.setDevice(bitmapResult); // drawXY will result on that Bitmap
c.drawBitmap(bitmapOld, left, top, paint);
how to get the drawable from Bitmap: by using the BitmapDrawable-Subclass which extends Drawable, like this:
Bitmap myBitmap = BitmapFactory.decode(path);
Drawable bd = new BitmapDrawable(myBitmap);
The bitmap you are retrieving is immutable, meaning it can't be modified. Although it doesn't specify on the Canvas page that the constructor needs a mutable bitmap, it does.
To create a mutable bitmap, you can use this method.