1

我需要创建一个 pdf 页面上传到 cloudinary。我发现 PDFJet 效果很好。pdf 包含一页或两页,每页有一张图像。每个页面都包含一个 WebView 和一个在其上绘制的 Canvas 位图。要将 WebView 转换为位图,我将capturePicture()其保存为图片,然后使用以下方法将其转换为位图pictureDrawable2Bitmap。然后我把画布变成一个位图。使用方法将两者叠加成一个新的位图overlayBitmap

然后对第二个位图重复此操作。位图被压缩并转换为字节数组,因此可以使用 PDFJet 的图像对象将它们添加到 PDF 中。创建时我得到了 OutOfMemory Image image。我尝试在使用后回收每个位图并删除 pdf 文件,但没有帮助。此外,上传的 png 质量较低。

关于使用 PDFJet 从位图创建 pdf 的更好方法的任何建议?

private class CloudinaryTask extends AsyncTask <Bitmap, Void, Object> {

     ProgressDialog progressBar;

     @Override
    protected Object doInBackground (Bitmap...bmp) {            

    tStamp = String.valueOf(System.currentTimeMillis());

        // Scaling used to match canvas drawing to the webview
    Matrix matrix = new Matrix();
    matrix.postScale(1.5f, 1.5f);   

    // Get the contents of the webview and turn it into a bitmap
    Bitmap bmp1 = pictureDrawable2Bitmap(new PictureDrawable(picture));

    // Create a new bitmap from the canvas to match the webview contents
    Bitmap resizedBitmap = Bitmap.createBitmap(DrawingView.canvasBitmap, 0, 0, DrawingView.canvasBitmap.getWidth(), DrawingView.canvasBitmap.getHeight(), matrix, false);

    // Turn the new bitmap into an InputStream to upload to Cloudinary
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    overlayBitmap = overlayMark(bmp1, resizedBitmap);
    bmp1.recycle();
    resizedBitmap.recycle();
    overlayBitmap.compress(CompressFormat.PNG, 100, bos); 

    byte[] bitmapdata = bos.toByteArray();
    overlayBitmap.recycle();
    bs = new ByteArrayInputStream(bitmapdata);

    // If there's a 2nd page or if the user clicked 'flip form over', creates a bitmap of that image
    if (pagetwo || flipPage) {
        if (!flipPage) {
            Bitmap bmp2 = pictureDrawable2Bitmap(new PictureDrawable(picture2));
            ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
            bmp2.compress(CompressFormat.PNG, 100, bos2); 
            bmp2.recycle();
            byte[] bitmapdata2 = bos2.toByteArray();
            bs2 = new ByteArrayInputStream(bitmapdata2);
        }
        else {
            Bitmap bmp2 = pictureDrawable2Bitmap(new PictureDrawable(picture2));
            System.out.println(bmp2.getWidth());
            System.out.println(bmp2.getWidth());
            Bitmap resizedBitmap2 = Bitmap.createBitmap(DrawingViewBack.canvasBitmap, 0, 0, DrawingViewBack.canvasBitmap.getWidth(), DrawingViewBack.canvasBitmap.getHeight(), matrix, false);
            overlayBitmap2 = overlayMark(bmp2, resizedBitmap2);
            bmp2.recycle();
            resizedBitmap.recycle();

            ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
            overlayBitmap2.compress(CompressFormat.PNG, 100, bos2); 
            overlayBitmap2.recycle();
            byte[] bitmapdata2 = bos2.toByteArray();
            bs2 = new ByteArrayInputStream(bitmapdata2);
        }
    }

    // Creates pdf of one or both pages
    PDF pdf;
    File f = new File(Environment.getExternalStorageDirectory(), "offer.pdf");
    try {
        FileOutputStream out = new FileOutputStream(f);
        pdf = new PDF(out);
        float[] aspect = new float[2];
        // Set the height and width of each pdf page
        aspect[0] = (float) overlayBitmap.getWidth();
        aspect[1] = (float) overlayBitmap.getHeight();

        // Create the first page and set the first image
        Page page = new Page(pdf, aspect);
        Image image1 = new Image(pdf, bs, ImageType.PNG);
        image1.setPosition(0, 0);
        image1.drawOn(page);
        if (pagetwo || flipPage) {
            // Create the second page and set the 2nd image
            Page page2 = new Page(pdf, aspect);
            Image image2 = new Image(pdf, bs2, ImageType.PNG);
            image2.setPosition(0, 0);
            image2.drawOn(page2);
        }
        pdf.flush();

        // Turn the pdf file into an inputstream so cloudinary can use it
        FileInputStream fileInputStream=null;
        byte[] bFile = new byte[(int) f.length()];
        try {
            //convert file into array of bytes
            fileInputStream = new FileInputStream(f);
            fileInputStream.read(bFile);
            fileInputStream.close();
            f.delete();
        }catch(Exception e){
            e.printStackTrace();         
        }

        bs3 = new ByteArrayInputStream(bFile);

    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    } catch (Exception e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

    // Create a map and add some required parameters for cloudinary
    Map<Object, Object> cloud = new HashMap<Object, Object>();
    cloud.put("cloud_name", "");
    cloud.put("api_key", "");
    cloud.put("api_secret", "");

    try {
        // Upload to cloudinary
        Cloudinary cloudinary = new Cloudinary(cloud);
        cloudinary.uploader().upload(bs3, Cloudinary.asMap("public_id", Network.imageUrl + tStamp));
4

0 回答 0