I need to convert a specific view (UI) into an image (eg. jpeg or png)... to achieve this i have thought to crop view through programming. I have come across many good links over here, but all of them deal with Bitmap or photo itself. Please guide me with a clue to achieve this effect.
Please find the attached screenshot below.
.
Thanks!
Actually I found the answer:
public static Bitmap loadBitmapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
Related
I'm trying to take a screenshot of the contents of a LinearLayout. The layout contains a scrollview that can be of variable height/width. This code works fine when the layout is not too big (i.e. you don't need to scroll very much off screen to see everything):
View v1 = (LinearLayout)theLayout;
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
However, if the LinearLayout that I'm trying to capture is large, then the application crashes with a null pointer on v1.getDrawingCache().
There is an error in logcat:
05-11 13:16:20.999: W/View(17218): View too large to fit into drawing
cache, needs 4784400 bytes, only 3932160 available
How can I properly take a screenshot of this layout? Is there another way to go about it that doesn't use so much memory?
Here's what ended up working for me to capture a large off-screen view in it's entirety:
public static Bitmap loadBitmapFromView(View v)
{
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
It's similar to the answer here, except that I use the view's width/height to create the bitmap
As the original question is talking about a ScrollView I just though I would add the following for anyone that was having problems getting the full height. To get the height of the ScrollView, the v.getHeight doesn't work, you need to use:
v.getChildAt(0).getHeight()
so the full thing would be:
ScrollView scrollView = (ScrollView) result.findViewById(R.id.ScrlView);
loadBitmapFromView(scrollView);
public static Bitmap loadBitmapFromView(View v)
{
int height = v.getChildAt(0).getHeight()
int width = v.getWidth()
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
Just using v.getHeight just gets the screen height, not the full height of the ScrollView.
While trying to make a screen shot of a custom view,I got the IllegalArgumentException : width and height must be >0. Here is what I wrote :
bitmap = loadBitmapFromView(v1);//v1 the custom view I want to picture.
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
The line in which there is the exception : Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Of course the screen shot code is executed after the view is showed,on the next_act button code
The width and height of LayoutParams can be a constant such as FILL_PARENT, and they are negative numbers.
To get the view's measured dimensions you can use:
v.getMeasuredWidth() and v.getMeasuredHeight()
However, it depends on what you're after, and whether the view has been measured.
It would probably make more sense to wait until the view has been drawn, before taking the screenshot ;-) But you'd need to explain more about your use case.
I have researched to find solution to take a snapshot of current activity and I found 2 solutions to taking a snapshot, but all of them can't help me get right thing which I want.
My activity use camera, and I show 3D object in that layout (same as AR, but not use marker, just draw 3D object in camera frame).
So when I use found solutions, I just get Parent layout (RelativeLayout which I use to keep both camera and 3D object) with some other objects (some button), that bitmap same as physical design screen of XML file.
I used this solution to get snapshot:
private Bitmap takeScreenShot(Activity activity)
{
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return b;
}
and this solution:
public Bitmap snap() {
Bitmap bitmap = Bitmap.createBitmap(this.view.getWidth(), this.view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
But I got same result.
Please help me to find good solution to resolve this problem.
Thanks.
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);