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
}
Related
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;
}
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);
}
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;
}`
I'd like to use several CustomViews in my appwidget and thus want to create a bitmap out of every CustomView.
So i tried that with
Bitmap customBitmap = customView.getDrawingCache(true);
remoteViews.setImageViewBitmap(R.id.imageView1, customBitmap );
but it doesn't work at all.
Are there any suggestions?
yes, it is drawn on a canvas
I would do something like this:
public Bitmap createCustomView(){
Bitmap bitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// draw on the canvas:
// ...
return bitmap;
}
and then set the Bitmap to the ImageView:
remoteViews.setImageViewBitmap(R.id.imageView1, createCustomView() );
int x = 10;
int y = 10;
int r = 4;
Paint mPaint = new Paint();
mPaint.setColor(0xFF0000);
Canvas mCanvas = new Canvas();
mCanvas.drawCircle(x,y,r,mPaint);
Is there any way to convert mCanvas to a Drawable? My goal is to generate drawables with a certain shape and color.
Thanks
For simple shapes like your circle, I'd think a Shape Drawable would be easier. For more complicated things, just create a Bitmap for your Canvas to use, then create the Canvas and draw into it, then create a Drawable from your Bitmap. Something like:
int x = 10;
int y = 10;
int r = 4;
Paint mPaint = new Paint();
mPaint.setColor(0xFF0000);
Bitmap bitmap = Bitmap.createBitmap(/* read the docs*/);
Canvas mCanvas = new Canvas(bitmap);
mCanvas.drawCircle(x,y,r,mPaint);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
To be perhaps somewhat pedantic (and hopefully increase your understanding), a Canvas just hosts the "draw" calls and draws into a Bitmap that you specify. This means:
Your example code doesn't do much, since you didn't construct your Canvas with a Bitmap or call setBitmap() on it.
You're not converting your Canvas to a Drawable, you're constructing a Drawable from the Bitmap your Canvas calls draw into.
Taken from another post, here is the psuedo code to do this.
Image on canvas to JPEG file
ByteArrayOutputStream baos = new ByteArrayOutputStream()
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, baos);
// This converts the bitmap to a drawable
BitmapDrawable mDrawable = new BitmapDrawable(getResources(),bitmap);
Alternately, you could use getDrawingCache() as outlined in another answer of that thread.