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
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"));
}
}
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!
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());
}
}
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
I am using this API demo of the Developer site, THIS DEMO.
But i am wonder that how to save that image in to My Andrtoid Device.
Is please anyone give the Code to save that drawn image to the Android Device.
Thanks.
try this code
View content = your_view;
content.setDrawingCacheEnabled(true);
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = content.getDrawingCache();
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(path+"/image.png");
FileOutputStream ostream;
try {
file.createNewFile();
ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.flush();
ostream.close();
Toast.makeText(getApplicationContext(), "image saved", 5000).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "error", 5000).show();
}
drawView.setDrawingCacheEnabled(true);
Bitmap bm = null;
drawView.destroyDrawingCache();
bm=drawView.getDrawingCache();
Then write the bitmap to file using bitmap factory.
One option is create another Canvas (as shown below) and repeat all your drawing on this new canvas.
Once done, call drawBitmap.
Bitmap bitmap = new Bitmap(// Set the params you like //);
Canvas canvas = new Canvas(bitmap);
// Do all your drawings here
canvas.drawBitmap(// The first picture //);
The best would be if there was a way to copy an existing canvas and then you wont need to re-draw everything but I couldn't find one.
I have implemented the below approach & worked for me.
Get your CustomView by using its id from xml file but not by instantiating the Customview.
View v = findViewById(R.id.custom_view);
//don't get customview by this way, View v = new CustomView(this);
int canvasWidth = v.getWidth();
int canvasHeight = v.getHeight();
Bitmap bitmap = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
ImageView imageView = findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);
All code should be inside saveButton click listener.