Can not take texture view screenshot in android - 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;
}

Related

Combine Images in Canvas and then view it through ImageView using Android Studio

I want to combine images by putting them on top of eachother in Canvas and then view the combination using ImageView. What I know is if you set the first Bitmap into the Canvas everything else you do will be added to that first Bitmap. I only get the error:
PointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
ImageView mainImage = (ImageView) view.findViewById(R.id.mainImage);
Bitmap bottomImage = BitmapFactory.decodeFile("image1.png");
Bitmap topImage = BitmapFactory.decodeFile("image2.png");
Bitmap tempBitmap = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(bottomImage, 0, 0, null);
tempCanvas.drawBitmap(topImage, 0, 0, null);
mainImage.setImageBitmap(bitmap);
Try Below Code
public Bitmap drawImageOnImage(Bitmap background, Bitmap pic, int x_to_draw, int y_to_draw, int width, int height)
{
try
{
Canvas newCanvas = new Canvas(background);
Bitmap img_bitmap = Bitmap.createScaledBitmap(pic, width, height, true);
newCanvas.drawBitmap(img_bitmap, x_to_draw, y_to_draw,new Paint());
}catch (Exception e)
{
System.out.println("Error Occured=>");
e.printStackTrace();
}
return background;
}
Here function takes 2 input bitmaps, Background bitmap and another image bitmap to draw over background bitmap. You will get combined bitmap to set for ImageView. X and Y are the co-rdinates where top image bitmap start paint. Same for height and width
After some experiments this code worked perfectly. And I added multiple top images.
private void drawAndView(ImageView imgView, Bitmap bottomImage, Bitmap[] topImages){
Bitmap tempBitmap = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(bottomImage, 0, 0, null);
for (Bitmap bitmap : topImages){
tempCanvas.drawBitmap(bitmap, 0, 0, null);
}
imgView.setImageBitmap(tempBitmap);
}

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
}

Saving a canvas in Android does not work every time

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

Create new bitmap with black remain region issue

I have to create new bitmap from an original image after user do somethings with it like zooming, rotating, dragging. And i have a frame border, and only part of original image lay on frame is picked. Issue is that remain region of frame is black, and i want it could be transparent or white. How to do that? Thanks in advance.
public Bitmap createBitmap(final Matrix pMatrix, final Bitmap pSourceBitmap){
Bitmap bmp = Bitmap.createBitmap(width, height, pSourceBitmap.getConfig());
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(pSourceBitmap, pMatrix, new Paint());
return bmp;
}
To make the remaining portion transparent
use the following code
public Bitmap createBitmap(final Matrix pMatrix, final Bitmap pSourceBitmap)
{
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp= Bitmap.createBitmap(width, height, conf);
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(pSourceBitmap, pMatrix, new Paint());
return bmp;
}

bitmap from canvas to OpenGL | Why does this code work in 2.1, but not 2.2 and later androids?

I got a code (by Google) like this :
InputStream is = mContext.getResources().openRawResource(R.drawable.icon);
bitmap = BitmapFactory.decodeStream(is);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
I need to pick an image of SpecialViewGroup (I decided to use Canvas), i've tried a code like this
//-- MY CODE
Bitmap bitmap = Bitmap.createBitmap(400, 840, Bitmap.Config.ARGB_8888);
bitmap = SpecialViewGroup.GetViewGroupBitmap;
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
// SpecialViewGroup.GetViewGroupBitmap(), class extends ViewGroup Code:
public static Bitmap getDesktopBitmap (){
Canvas canvas;
Bitmap bitmap = Bitmap.createBitmap(400, 840, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
this.draw(canvas);
return bitmap;
}
//-- END OF MY CODE
But I get errors in android 2.2 or higher, while in 2.1 it works correctly. Any Ideas?

Categories

Resources