滚动视图到PDF视图,在Android中产生模糊



>我有很长的滚动视图几乎 50+ 字段。

我正在将该滚动视图转换为 pdf 视图.pdf并创建所有完成的内容。但是显示数据的pdf文件是模糊的(太模糊(。

通过一些合并,我提供了一些参考图像(我的意思是输出图像(。

输出图像

我的代码:

private void takeScreenShot() {
    try {
    //here getScroll is my scrollview id
    View u = ((Activity) mContext).findViewById(R.id.getScroll);
    ScrollView z = (ScrollView) ((Activity) mContext).findViewById(R.id.getScroll);
    int totalHeight = z.getChildAt(0).getHeight();
    int totalWidth = z.getChildAt(0).getWidth();
    Bitmap bitmap = getBitmapFromView(u,totalHeight,totalWidth);
    Image image;
    //Save bitmap
    String path = Environment.getExternalStorageDirectory()+"/Folder/";
    String fileName = "report1.pdf";
    File dir = new File(path);
    if (!dir.exists())
        dir.mkdirs();
    Log.v("PDFCreator", "PDF Path: " + path);
    File myPath = new File(path, fileName);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
        image = Image.getInstance(stream.toByteArray());
        image.setAbsolutePosition(0, 0);
        Document document = new Document(image);
        PdfWriter.getInstance(document, new FileOutputStream(myPath));
        document.open();
        document.add(image);
        document.close();

    } catch (Exception i1) {
        i1.printStackTrace();
    }
}

有什么帮助吗?

试试这段代码:

    View u = ((Activity) mContext).findViewById(R.id.getScroll);
    u.setDrawingCacheEnabled(true);
    u.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(u.getDrawingCache());
    bitmap.setHasAlpha(true); //important for PNG
  ---------------DO SAVE WORK-----------------
    v1.setDrawingCacheEnabled(false);

而在

bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
将 10

更改为 90 或 100。它是图像质量。

或者只是将代码更改为更高的质量。

尝试另一种解决方案:

ScrollView v= (ScrollView)findViewById(R.id.getScroll);
int height = v.getChildAt(0).getHeight()
int width = v.getWidth()
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

使用此代码拍摄屏幕截图,然后

   ScrollView iv = (ScrollView) findViewById(R.id.scrollView);
    Bitmap bitmap = Bitmap.createBitmap(
            iv.getChildAt(0).getWidth(),
            iv.getChildAt(0).getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmap);
    c.drawColor(Color.WHITE);
    iv.getChildAt(0).draw(c);
    // Do whatever you want with your bitmap
    saveBitmap(bitmap);

并将位图另存为图像

public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }}

我可能会迟到,但无论如何。改用 png 格式,将质量提高到 100 并替换

View u = ((Activity) mContext).findViewById(R.id.getScroll); 

View u = ((Activity) mContext).findViewById(R.id.yourfirstscrollviewchild);

那会解决问题。

最新更新