android save text drawed bitmap to jpeg file - android

I try to make an android app
and it must contain bitmap to jpeg file method
Here is what i have to do
1.create BitmapDrawable object
BitmapDrawable bitmapDrawable = (BitmapDrawable)res.getDrawable(R.drawable.my_pic);
2.create Bitmap object
Bitmap bit = bitmapDrawable.getBitmap();
3.create set Bitmap on canvas object
canvas.drawBitmap(bit,0,0,null)
4.drawText "helloworld" on canvas
Paint pnt = new Paint();
pnt.setColor(Color.BLACK);
canvas.drawText("helloworld",100,100,pnt);
canvas.save();
canvas.restore();
and this code is what I tried to do to save Bitmap(or canvas) to Jepg File
a.
bit.compress(Bitmap.CompressFormat.JEPG, 100 , fileOutputStream);
but this can save original image(without text)
so, I used code b.
b.
view.setDrawingCacheEnabled(true);
Drawable drawable =getResources().getDrawable(R.drawable.my_pic);
view.layout(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
view.bulidDrawingCache(true);
Bitmap save_bit = view.getDrawingCache();
save_bit.compress(Bitmap.CompressFormat.JPEG, 100, fos);
and this cause nullpoint exception, and logcat send me a message 'View too large to fit into drawing cache, needs 31325840 bytes, only 3686400 available'
what do I do to save text drawed bitmap to file
please give me your opinion

Related

How to know that Canvas has used bitmap object

Hi I am creating Bitmap object and using in canvas, like below code
Bitmap result = Bitmap.createBitmap(loadedImage.getWidth(), loadedImage.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mCanvas.drawBitmap(blurBitmap, 0, 0, null);
mCanvas.drawBitmap(loadedImage, 0, 0, paint);
paint.setXfermode(null);
ivBlur.setImageBitmap(result);
// now i don't want Bitmap "result" so I am recycling it
if (result != null && !result.isRecycled())
result.recycle();
}
But from above code I am getting an error like
trying to use recycled bitmap android
How to fix "canvas: trying to use a recycled bitmap error"?
trying to use a recycled bitmap android.graphics.Bitmap
Canvas: trying to use a recycled bitmap android.graphics.Bitmap in android
If I don't write last three lines it is working perfect,
It may be because Canvas is using Bitmap "result" object and I am recycling "result"
Because of this bitmap is taking huge space, I have to recycle this bitmap otherwise I will get
java.lang.OutofMemoryError: bitmap size exceeds VM budget.
So does anyone know how can I get that, "Canvas has used bitmap object and now I can recycle result object".
The issue isn't the the Canvas has used the bitmap. The issue is that you passed the Bitmap to an ImageView that needs it to draw itself Once you pass that Bitmap to an ImageView, you can't recycle it for as long as the ImageView is alive.
You can recycle the bitmap only after the ImageView with the Bitmap is removed from the view hierarchy and never used again. In that case, it's easier to just let the garbage collector handle it.
If you instead need a smaller Bitmap, you should create and draw into that smaller Bitmap before sending it to the ImageView.

Cannot draw on bitmap loaded canvas

I am trying to create an image save and retrieve feature in android. The code I have for creating a jpg file from canvas is as below
Bitmap bitmap = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
I am trying to read the jpg file and create image on canvas using
Bitmap bMap = BitmapFactory.decodeStream(buf);
Bitmap workingBitmap = Bitmap.createBitmap(bMap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
view.mybitmap = mutableBitmap;
view.onDraw(view.Canvas);
The code I have in onDraw is
canvas.drawBitmap(view.mybitmap, 0, 0, view.myPaint);
This draws the bitmap on canvas correctly from the stored jpg file but I am not able to draw anything on the canvas after that. Has it loaded a immutable bitmap image on canvas which I am not able to edit?
Any help will be appreciated! Thanks!
You shouldn't need to be calling the ondraw method directly like you are doing currently. For a custom view class all you should need to be calling is view.invalidate(); This will auto call the ondraw method and re-draw the canvas. You will probably need to update the logic in your ondraw method to handle the cases where you want to draw additional things on the canvas.

Android clip Picture to Bitmap. Is Possible?

I have a Picture object, loaded from an SVG file, and I have set hardwareAccelerated=false to make it works on all devices.
Since there is a bug on android 4.0.4, I have to convert the Picture to Bitmap and I do that, in this way:
...
...
public void draw(Canvas canvas) {
...
...
//myPicture size is 9000x5000 but I want to display only this portion
clipRect.set(50, 50, 370, 530);
Bitmap bmp = getBitmapFromPicture(myPicture, clipRect);
canvas.drawBitmap(bmp, 0, 0, null);
bmp.recycle();
...
...
}
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect) {
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()), Math.round(clipRect.height()), Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(picture);
}
Now I want to clip the Picture because I want to display only the visible screen part of it.
But the canvas.drawPicture does not accept srcRect parameter.
How is it possible to achieve this?
EDIT:
By translate the canvas: canvas.translate(-50, -50) it seems that translate the bitmap, too.
You need to set a transform on the Canvas.
To move the portion of the picture at 50,50 down so it is on the bitmap (ie. at 0,0), just do:
canvas.translate(-50, -50);
So your method becomes:
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect)
{
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()),
Math.round(clipRect.height()),
Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-clipRect.left, -clipRect.top);
canvas.drawPicture(picture);
}

Modifying Bitmap from resources

I'm trying to draw some addition to my ImageView's content, which is represented by Drawable from resources. I know that all Drawables from resources are immutable by default, but even after I call mutable() method and get Bitmap from this Drawable to pass it to Canvas object I get java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor. Here is the code:
Drawable homeImage = mHomeImage.getBackground(); // my ImageView
homeImage.mutate();
Canvas c = new Canvas(((BitmapDrawable)homeImage).getBitmap());
Is there any way to modify this Bitmap without creating it's copy?
Try this
Bitmap bmp1 = (((BitmapDrawable)homeImage).getBitmap())
.copy(Config.ARGB_8888, true);
Canvas c = new Canvas(bmp1);
You will need to create a new bitmap for the canvas. As the canvas uses the bitmap we pass to store whats drawn, the bitmap should be mutable.

Print Bitmaps onto other bitmap android

I am trying to make my game a bit easier on the phone, so I am trying to figure out a way to print a bunch of bitmaps onto another big one, so I can just do it once, rather than every time the screen is redrawn. So, is there any way to do this? I know there is a way to print everything that is printed to the canvas to a bitmap, but I can't seem to get that to work. If that is the only way can someone explain how to do that? Thanks in advance.
Here is something I tried, but it didn't work out so well
Bitmap background;
Canvas canvas;
private void methodName() {
background = Bitmap.createBitmap(width, height, someKindOfConfigThing);
canvas = new Canvas(background);
canvas.drawBitmap(blahblah);
}
What you would do is to create the main bitmap, attach that to a canvas to which you can draw.
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
Canvas c = new Canvas(bitmap);
You can draw (parts of) bitmaps to this canvas using
c.drawBitmap(anotherBitmap, transformMatrix, paint);
To attach the main bitmap to the view you would create a new ImageView, call setImageBitmap passing your main bitmap and set it as the current contentview using setContentView.
If you want to combine multiple bitmaps to another big one and reuse that, you already on the right way! Show us what you have done and tell us what the result is. I guess we can help you :)
[update] it should be possible to save this new bitmap to disc or store it temporarily as a variable:
private void methodName() {
background = Bitmap.createBitmap(width, height, someKindOfConfigThing);
canvas = new Canvas(background);
// drawing on the canvas should change the bitmap "background" too
canvas.drawBitmap(blahblah);
FileOutputStream fos = null;
try {
fos = new FileOutputStream("/path/to/image.png");
background.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
// catching...
}
}

Categories

Resources