Saving a canvas in Android does not work every time - android

I'm having this code:
public void saveimage() {
Bitmap bitmap = Bitmap.createBitmap(colorGFX.getWidth(),
colorGFX.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
canvas = colorGFX.canvas;
canvas.setBitmap(bitmap);
saveBitmap(bitmap);
}
colorGFX object extends a SurfaceView.
saveBitmap actually writes the image on the file.
The problem is that most of the times a WHITE image is saved, other times the correct image is saved.
Did I missed something, or why does the image saves only let's say 1/5 of the times?

This worked for me:
Bitmap bitmap = Bitmap.createBitmap(colorGFX.getWidth(),
colorGFX.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(colorGFX.bitmap, 0f, 0f, null);
canvas.drawBitmap(colorGFX.pictureBitmap, 0f, 0f, null);
saveBitmap(bitmap);

Try this:
public void saveimage() {
Bitmap bitmap = Bitmap.createBitmap(colorGFX.getWidth(),
colorGFX.getHeight(), Bitmap.Config.ARGB_8888);
colorGFX.draw(new Canvas(bitmap))
saveBitmap(bitmap);
}

Related

Can not take texture view screenshot in android

I have a texture view on which a video is getting played. I need to take screenshot of the frame of playing video. Earlier I was using a method
public Bitmap getFrameAsBitmap() {
Bitmap bmp = textureview.getBitmap();
Canvas canvas = new Canvas(bmp);
textureview.draw(canvas);
return bmp;
}
It is working fine on most of the devices but on Samsung M series I can not take screenshot. Only black screen is coming.
Then I tried
public Bitmap getFrameAsBitmap() {
View view = textureview;
view.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
textureview.draw(canvas);
return bmp;
}
But this method is not returning data on any phone. Any ideas what to do?
Here are several things that you can try.
Return the bitmap directly.
public Bitmap getFrameAsBitmap() {
return textureview.getBitmap();
}
Clone the bitmap then return the clone.
public Bitmap getFrameAsBitmap() {
Bitmap bmp = textureview.getBitmap();
Bitmap clone = bmp.copy(Bitmap.Config.ARGB_8888, true);
return clone;
}
Draw the bitmap on a new canvas.
public Bitmap getFrameAsBitmap() {
Bitmap bmp = textureview.getBitmap();
//Try using bitmap's width/height first, if it does not work, use view's width/height instead.
Bitmap newBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
//Bitmap newBmp = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBmp);
//It is not efficient to create a Paint object too often. Better make it as global variable.
canvas.drawBitmap(bmp, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG));
return newBmp;
}

How to find bitmap created from canvas is empty or not

Bitmap bitmap = Bitmap.createBitmap(canvasLL.getawidth(), canvasLL.getHeight(), Bitmap.Config.RGB_5);
Canvas canvas = new Canvas(bitmap)
View.draw(canvas);
This how i had create a bitmap using the things draw on canvas. But i want to check that if something draw or not on the canvas. Any help will be really appreciated.
Bitmap.getGenerationId() can be used to check if a bitmap has been modified.
Bitmap bitmap = Bitmap.createBitmap(canvasLL.getWidth(), canvasLL.getHeight(), Bitmap.Config.RGB_565);
final int oldBitmapId = bitmap.getGenerationId();
Canvas canvas = new Canvas(bitmap);
View.draw(canvas);
final int newBitmapId = bitmap.getGenerationId();
if (oldBitmapId != newBitmapId) {
// a Bitmap has been changed
}

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

Android : Can't get bitmap from a canvas

I am trying to get a bitmap from a canvas by the following functionality..But I am getting the bitmap width and height -1.....Can't make out why is it happening...I have attached the code snippet below...Please help!!
Thanks in advance :)
`private Bitmap createBitmapFromCanvas() {
Bitmap bitmap = Bitmap.createBitmap(panelWidth, panelHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas();
canvas.setBitmap(bitmap);
canvas.drawRect(new Rect(0,0, panelWidth, panelHeight), new Paint());
for (PathListForDraw points : finalPathList) {
canvas.drawPath(points.getmPath(), points.getmPaint());
// canvas.save();
}
return bitmap;
}`

How to paint on Image and save that image in to Android?

I am new to canvas. I want to use the My already saved Image and want some paint on that image. after that i want to save it.
I know that with using Canvas it is possible. I can able to do painting on the Image but while i am going to store that image it only saved the painting. Not the Image with painting.
So can anybudy tell me code of how to paint on image and save that image ?
Thanks.
Here is my Code that use to do paint on the SurfaceView.
Source Code:
#Override
public void run() {
//Canvas canvas = null;
while (_run){
try{
canvas = mSurfaceHolder.lockCanvas(null);
if(mBitmap == null){
mBitmap = Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
}
final Canvas c = new Canvas (mBitmap);
//canvas.drawColor(0, PorterDuff.Mode.CLEAR);
c.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawColor(Color.WHITE);
// Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
// if(!(DrawingActivity.imagePath==null)){
// canvas.drawBitmap(DrawingActivity.mBitmap, 0, 0, null);
// }
commandManager.executeAll(c);
canvas.drawBitmap (mBitmap, 0, 0,null);
} finally {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
I am using mBitmap to save the Bitmap to the SDCard.
Your problem is your drawing over and over on your entire canvas:
final Canvas c = new Canvas (mBitmap); // creates a new canvas with your image is painted background
c.drawColor(0, PorterDuff.Mode.CLEAR); // this makes your whole Canvas transparent
canvas.drawColor(Color.WHITE); // this makes it all white on another canvas
canvas.drawBitmap (mBitmap, 0, 0,null); // this draws your bitmap on another canvas
Use logic roughly like this:
#Override
public void run() {
Canvas c = new Canvas(mBitmap);
/* Paint your things here, example: c.drawLine()... Beware c.drawColor will fill your canvas, so your bitmap will be cleared!!!*/
...
/* Now mBitmap will have both the original image & your painting */
String path = Environment.getExternalStorageDirectory().toString(); // this is the sd card
OutputStream fOut = null;
File file = new File(path, "MyImage.jpg");
fOut = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
}
Also don't forget to add necessary permission to save your file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
outside <application></application> in your manifest file.

Categories

Resources