首页 > 解决方案 > 如何在android中获得高质量的位图?

问题描述

如何在android中获得高质量的位图?

我做了一个 webView,因为我们有 spring web 项目。我们想在PDA中使用该项目并用蓝牙打印机打印一些标签。所以我做了一个 webView,我要打印带有标签的网页。但是“window.print”功能不适用于蓝牙打印机。因此,我尝试使用屏幕截图在 webView 中打印。

可悲的是,当我通过 android 中的 Bitmap.createBitmap 函数获取屏幕的位图时,它的质量真的很差。因此,它的印刷质量很差。我想要更好的打印分辨率。

如何获得更好的屏幕位图分辨率?

@RequiresApi(api = Build.VERSION_CODES.O) public Bitmap createBitmapFromView(View view) {

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);


    //Pre-measure the view so that height and width don't remain null.
    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    //Assign a size and position to the view and all of its descendants
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());


    Bitmap bitmapa = Bitmap.createBitmap(width , height , Bitmap.Config.ARGB_8888);


    Canvas c = new Canvas(bitmap);

    view.draw(c);

    return bitmap;
}

标签: androidbitmap

解决方案


禁用位图缩放

Options options = new BitmapFactory.Options();
    options.inScaled = false;
    Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);

推荐阅读