I'm trying to implement the css3 page flip effect on a android/phonegap app. To do this, I need to dynamically save the current webview to png or jpeg so that it can be loaded to a div in the page flip html. I noticed the Picture class in android's docs but I'm not sure if that can be converted and saved. Could this be done through JS? Any ideas?
thanks
Try following code for capturing webview and saved jpg to sdcard.
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
Picture picture = view.capturePicture();
Bitmap b = Bitmap.createBitmap(
picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
picture.draw(c);
FileOutputStream fos = null;
try {
fos = new FileOutputStream( "/sdcard/" + "page.jpg" );
if ( fos != null ) {
b.compress(Bitmap.CompressFormat.JPEG, 90, fos );
fos.close();
}
}
catch( Exception e ) {
System.out.println("-----error--"+e);
}
}
});
webview.loadUrl("http://stackoverflow.com/questions/15351298/capturing-android-webview-image-and-saving-to-png-jpeg");
The easiest way (to my knowledge) to capture a View as an image is to create a new Bitmap and a new Canvas. Then, simply ask your WebView to draw itself on your own Canvas instead of the Activity's default one.
Pseudocode:
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
myWebView.draw(canvas);
//save your bitmap, do whatever you need
Related
I need to create .jpeg/.png file on my Android application programmatically. I have simple image (black background), and it need to write some text on it programmatically. How can I do it? Is it possible?
It's definately possible.
To write text on an image you have to load the image in to a Bitmap object. Then draw on that bitmap with the Canvas and Paint functions. When you're done drawing you simply output the Bitmap to a file.
If you're just using a black background, it's probably better for you to simply create a blank bitmap on a canvas, fill it black, draw text and then dump to a Bitmap.
I used this tutorial to learn the basics of the canvas and paint.
This is the code that you'll be looking for to turn the canvas in to an image file:
OutputStream os = null;
try {
File file = new File(dir, "image" + System.currentTimeMillis() + ".png");
os = new FileOutputStream(file);
finalBMP.compress(CompressFormat.PNG, 100, os);
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
screenGrabFilePath = file.getPath();
} catch(IOException e) {
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
Log.e("combineImages", "problem combining images", e);
}
Yes, see here
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
You can also use awt's Graphics2D with this compatibility project
Using Graphics2d you can create a PNG image as well:
public class Imagetest {
public static void main(String[] args) throws IOException {
File path = new File("image/base/path");
BufferedImage img = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.YELLOW);
g2d.drawLine(0, 0, 50, 50);
g2d.setColor(Color.BLACK);
g2d.drawLine(50, 50, 0, 100);
g2d.setColor(Color.RED);
g2d.drawLine(50, 50, 100, 0);
g2d.setColor(Color.GREEN);
g2d.drawLine(50, 50, 100, 100);
ImageIO.write(img, "PNG", new File(path, "1.png"));
}
}
Hello I am trying to create PDF from webview in Crosswalk but I didn't find any supporting methods how to do it. Currently I am using this option to create bitmap
How can I capture the whole view into a Bitmap when using crosswalk to display webpage?
and then I try to convert the bitmap using itext lib
How I can convert a bitmap into PDF format in android as output I get blank pdf (black page).
Here is the code for bitmap:
private void createBitmap (XWalkView view) {
String pathToFile = Environment.getExternalStorageDirectory() +"/bitmap.jpg";
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap btmp = view.getDrawingCache();
Canvas canvas = new Canvas(btmp);
Paint paint = new Paint ();
int height = btmp.getHeight();
canvas.drawBitmap(btmp, 0, height, paint);
view.draw(canvas);
try {
OutputStream fOut = null;
File file = new File(pathToFile);
fOut = new FileOutputStream(file);
btmp.compress(Bitmap.CompressFormat.JPEG, 50, fOut);
fOut.flush();
fOut.close();
btmp.recycle();
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
} catch (Exception e) {
e.printStackTrace();
}
}
I need just hint :). Thanks a lot. This is my first question I apologize for typos
XWalkView use a standalone SurfaceView(or TextureView) as its output target.The draw or onDraw of XWalkView draws nothing. So you can not rely on it to draw something into a pdf file.
As a workaround, there is another api XWalkView.captureBitmapAsync which can be used to capture the visible webpage into a bitmap. Here is a simple guide about how to use it:
1), Implement XWalkGetBitmapCallback
import org.xwalk.core.XWalkGetBitmapCallback;
class XWalkGetBitmapCallbackImpl extends XWalkGetBitmapCallback {
public XWalkGetBitmapCallbackImpl() {
super();
}
//Note: onFinishGetBitmap happens at the same thread as captureBitmapAsync, usually the UI thread.
#Override
public void onFinishGetBitmap(Bitmap bitmap, int response) {
//if response == 0, save this bitmap into a jpg file //otherwise errors.
}
}
2), Start the capture in UI thread:
private void captureContent() {
if ( xWalkView == null) return;
mXWalkGetBitmapCallback = new XWalkGetBitmapCallbackImpl();
xWalkView.captureBitmapAsync(mXWalkGetBitmapCallback);
}
More details is here: https://crosswalk-project.org/jira/browse/XWALK-5110
Hey I'm trying to draw a webview to a bitmap the following way:
CustomWebView webView = (CustomWebView) findViewById(R.id.chart_webview_renderer);
String capturePathString = Environment.getExternalStorageDirectory().getPath() + "/temp/ms_" + System.currentTimeMillis() + ".png";
Bitmap bm = Bitmap.createBitmap(webView.getMeasuredWidth(), webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas bigcanvas = new Canvas(bm);
Paint paint = new Paint();
int iHeight = bm.getHeight();
bigcanvas.drawBitmap(bm, 0, iHeight, paint);
webView.draw(bigcanvas);
if (bm != null) {
try {
OutputStream fOut = null;
File file = new File(capturePathString);
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 50, fOut);
fOut.close();
fOut.flush();
bm.recycle();
} catch (Exception e) {
e.printStackTrace();
}
}
This works fine on the tablets we have available for testing here (Galaxy Tab 2 and 3). But results in a white bitmap of the correct size on a Sony Xperia Z and Samsung Galaxy S2.
The webpage it is trying to draw to the bitmap contains ONLY an HTML5 canvas and when I also add normal HTML to it, it will draw just that HTML to the bitmap just fine.
The webview is set as invisible behind all views, although I have tried making it visible and on top of all views which produced no different results.
I wasn't able to solve this issue with the original method I used. So I added the following javascript fix to the html: https://code.google.com/p/todataurl-png-js/ to allow the use of
canvas.toDataUrl()
which returns a Base64 encoded PNG image. I then used
WebView.addJavascriptInterface
to allow the javascript to send the base64 encoded image to java, which then saved it on the device.
A rough example of what I did is this:
// After initializing the webview:
JavaScriptInterface jsInterface = new JavaScriptInterface();
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(jsInterface, "android");
// The class used as javascript interface for saving the image to a file
public class JavaScriptInterface {
#JavascriptInterface
public void canvasToImage(String base64ImageData){
String capturePathString = Environment.getExternalStorageDirectory().getPath() + "/temp/ms_" + System.currentTimeMillis() + ".png";
try{
File file = new File(capturePathString);
file.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(file);
byte[] decodedString = android.util.Base64.decode(base64ImageData, android.util.Base64.DEFAULT);
fos.write(decodedString);
fos.flush();
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
// In the javascript it looks something like this
function canvasToImage(){
var dataUrl = canvas.toDataURL();
window.android.canvasToImage(dataUrl.replace("data:image/png;base64,", ""));
}
It isn't as clean as I'd hope it would be, but it works on all devices here now!
This question already has answers here:
Convert view to bitmap on Android
(9 answers)
Closed 1 year ago.
I have posted same question but it's in near to my problem that's why i have posted it second time
Hi i want to capture image from RelativeLayout, for that i have used below code
captureRelativeLayout.setDrawingCacheEnabled(true);
bitmap = captureRelativeLayout.getDrawingCache(true).copy(
Config.ARGB_8888, false);
the problem is that when i start activity and get image from that view at that time it will work fine, but if i use it second time, the image is not being refreshed, means that previous bitmap is every time i getting.
Now if i close my activity and agian start it then i will get updated image but again not in second time
:( for more information look at the
can't share image properly android
Here are two ways to convert a view to a bitmap:
Use Drawing Cache:
RelativeLayout view = (RelativeLayout)findViewById(R.id.relativelayout);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheEnabled(false);
I've had some issue with the drawing cache method when the view is very large (for example, a TextView in a ScrollView that goes far off the visable screen). In that case, using the next method would be better.
Use Canvas:
RelativeLayout view = (RelativeLayout)findViewById(R.id.relativelayout);
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
You should destroy the drawing cache after copying it, so the cache will be built again next time you call getDrawingCache().
The code would look like this:
captureRelativeLayout.setDrawingCacheEnabled(true);
bitmap = captureRelativeLayout.getDrawingCache(true).copy(Config.ARGB_8888, false);
captureRelativeLayout.destroyDrawingCache();
Or like this if you don't want to enable the flag:
captureRelativeLayout.buildDrawingCache(true);
bitmap = captureRelativeLayout.getDrawingCache(true).copy(Config.ARGB_8888, false);
captureRelativeLayout.destroyDrawingCache();
Reference: https://groups.google.com/d/msg/android-developers/IkRXuMtOA5w/zlP6SKlfX-0J
There is a Kotlin extension function in Android KTX:
val config: Bitmap.Config = Bitmap.Config.ARGB_8888
val bitmap = canvasView.drawToBitmap(config)
finally i got solution from this view.getDrawingCache() only works once
i just forget to put
captureRelativeLayout.setDrawingCacheEnabled(false);
i have done it by below code,
try {
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
captureRelativeLayout.setDrawingCacheEnabled(true);
File sdcard = Environment.getExternalStorageDirectory();
File f = new File(sdcard, "temp.jpg");
FileOutputStream out = null;
out = new FileOutputStream(f);
captureRelativeLayout.setDrawingCacheEnabled(true);
bitmap = captureRelativeLayout.getDrawingCache(true).copy(
Config.ARGB_8888, false);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
captureRelativeLayout.setDrawingCacheEnabled(false);
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); // imageUri
sharingIntent.setType("image/jpg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); // imageUri
startActivity(Intent.createChooser(sharingIntent, "Share Image"));
} catch (Exception e) {
e.printStackTrace();
}
As DrawingCache is depracated then in kotlin:
fun View.createBitmap(): Bitmap {
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
Canvas(bitmap).apply {
background?.draw(this) ?: this.drawColor(Color.WHITE)
draw(this)
}
return bitmap
}
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
"bitmap" is the final bitmap..
I am programmatically drawing to a canvas using data entered by the user. Once all of the data is entered, the user can flip through the images and they will be drawn to the canvas. The user has the option to save all of the images(could be several hundred). I use a runnable that runs on the UI thread that will draw and save each image(since you can't draw to a canvas from an AsyncTask). This works, but the problem I am having is if while the saving is going on, the user turns off the screen or minimizes the app. This causes the jpegs to just be black. I would like the saving to be something that could run in the background and still work.
Code used to draw to canvas and save image:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap bitmap;
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
// draw everything here
OutputStream stream = new FileOutputStream(imageName + ".jpg");
bitmap.compress(CompressFormat.JPEG, 100, stream);
stream.close();
}
So, is there a way for images to be drawn and saved to a file in the background while the canvas is not visible? Any help would be appreciated!
I think you can use this:
signLayout.setDrawingCacheEnabled(true); //signLayout is layout that contains image you want to save
// Save image from layout
String path = "sdcard0/test/test.jpg"; //file path you want to save
gestureSign.save(signLayout, path);
SAVE METHOD
//Save to path
public void save(View v, String SavePath)
{
if(mBitmap == null)
{
mBitmap = Bitmap.createBitmap (layoutSign.getWidth(), layoutSign.getHeight(), Bitmap.Config.RGB_565);;
}
Canvas canvas = new Canvas(mBitmap);
try
{
FileOutputStream mFileOutStream = new FileOutputStream(SavePath);
v.draw(canvas); //Lay toan bo layout
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, mFileOutStream);
mFileOutStream.flush();
mFileOutStream.close();
}
catch(Exception e)
{
Log.v("log_tag", e.toString());
}
}