Scrollview to PDF查看Android中的模糊

问题描述 投票:0回答:3

我有很长的scrollview几乎50多个字段。

我正在将该scrollview转换为pdf视图。 pdf也创造了所有完成。但显示数据的pdf文件模糊(太模糊)。

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

output image

我的代码:

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();
    }
}

有帮助吗?

android pdf scroll screenshot blur
3个回答
0
投票

试试这段代码:

    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);

0
投票

使用此代码拍摄屏幕截图

   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);
    }}

0
投票

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

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

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

那就行了。

© www.soinside.com 2019 - 2024. All rights reserved.