webview长截屏截图

public void printPDF() {
    String name = webView.getTitle() + ".pdf";
    PrintManager printManager = (PrintManager) getActivity().getSystemService(Context.PRINT_SERVICE);
    PrintAttributes.Builder builder = new PrintAttributes.Builder();
    builder.setColorMode(PrintAttributes.COLOR_MODE_COLOR);
    printManager.print("Print", (PrintDocumentAdapter) webView.createPrintDocumentAdapter(name), builder.build());
}


public Bitmap viewShot(final View view) {
    if (view == null)
        return null;
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(measureSpec, measureSpec);
    if (view.getMeasuredWidth() <= 0 || view.getMeasuredHeight() <= 0) {
        return null;
    }
    Bitmap bm;
    try {
        bm = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    } catch (OutOfMemoryError e) {
        System.gc();
        try {
            bm = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError ee) {
            return null;
        }
    }
    Canvas bigCanvas = new Canvas(bm);
    Paint paint = new Paint();
    int iHeight = bm.getHeight();
    bigCanvas.drawBitmap(bm, 0, iHeight, paint);
    view.draw(bigCanvas);
    return bm;
}

private Bitmap captureX5WebViewUnsharp(Context context, X5WebView webView) {
    if (webView == null) {
        return null;
    }
    if (context == null) {
        context = webView.getContext();
    }
    int width = webView.getContentWidth();
    int height = webView.getContentHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    webView.setHorizontalScrollBarEnabled(false);
    webView.setVerticalScrollBarEnabled(false);
    if (webView.getX5WebViewExtension() == null) {
        return null;
    }
    webView.getX5WebViewExtension().snapshotWholePage(canvas, false, false);
    return bitmap;
}


private void saveBitmapForPdf(List<Bitmap> bitmaps, String appDir, String name) {
    PdfDocument doc = new PdfDocument();
    int pageWidth = PrintAttributes.MediaSize.ISO_A4.getWidthMils() * 72 / 1000;

    float scale = (float) pageWidth / (float) bitmaps.get(0).getWidth();
    int pageHeight = (int) (bitmaps.get(0).getHeight() * scale);

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    for (int i = 0; i < bitmaps.size(); i++) {
        PdfDocument.PageInfo newPage = new PdfDocument.PageInfo.Builder(pageWidth, pageHeight, i).create();
        PdfDocument.Page page = doc.startPage(newPage);
        Canvas canvas = page.getCanvas();
        canvas.drawBitmap(bitmaps.get(i), matrix, paint);
        doc.finishPage(page);
    }
    File file = new File(appDir, name);
    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(file);
        doc.writeTo(outputStream);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

        doc.close();
        try {
            if (outputStream != null) {
                outputStream.close();
            }
            ShareUtils.shareFileToWeiXin(getActivity(), appDir, name);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


/**
 * 保存webview长截屏
 */
public void saveImage(X5WebView view) {

    Bitmap bitmap = captureX5WebViewUnsharp(getActivity(), view);
    if (bitmap == null) {
        //使用的是系统内核
        Picture snapShot = webView.capturePicture();
        bitmap = Bitmap.createBitmap(snapShot.getWidth(),
                snapShot.getHeight(), Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(bitmap);
        snapShot.draw(canvas);
    }

    try {
        //context.getExternalFilesDir(null).getPath()
        String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + Constants.FILE_NAME + "/webview_capture4.jpg";
        FileOutputStream fos = new FileOutputStream(fileName);
        List<Bitmap> bitmaps = new ArrayList<>();
        bitmaps.add(bitmap);
        saveBitmapForPdf(bitmaps, Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + Constants.FILE_NAME, "a.pdf");
        //压缩bitmap到输出流中
        bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bitmap != null) {
            bitmap.recycle();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值