chromium39提供了异步截图的api(java): ContentReadbackHandler.java, ContentReadbackHandler保存在ContentViewRenderView对象中。
java代码保存截图文件
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
private static int num = 0;
public void onFinishGetBitmap(Bitmap bitmap) {
if (null == bitmap) return;
File f = new File("/sdcard/webview-screenshot-" + (num++) + ".png");
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d("siyt", "siyt(0305-1): IOException:"+e.toString());
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
c++中保存截图代码
bool OnSnapshotTaken(bool success, const SkBitmap& bitmap)
static unsigned int num = 100;
char png_file_name[50] = { 0 };
sprintf(png_file_name, "/sdcard/screen_shot_%d.png", num++);
bool result = SkImageEncoder::EncodeFile(png_file_name, bitmap,
SkImageEncoder::kPNG_Type,80);
LOG(ERROR)<<"png_file_name="<<png_file_name;
if(result) return true;
else return false;
}