PDF jpeg image compression for DCTDecode

34
February 12, 2019, at 05:20 AM

I'm using android pdf writer apw-library to generate a pdf document.

Library works well but i want a small pdf size so i'm using compressed jpeg instead of bitmaps.

But apw-library accepts only bitmaps for image input. https://github.com/Turbo87/apwlibrary/blob/master/apwlibrary/src/main/java/crl/android/pdfwriter/XObjectImage.java

Looking at source code i tried to modify it:

Original method : uses Filter [/ASCII85Decode /FlateDecode]

private byte[] getBitmapData(Bitmap bitmap) {
        byte[] data = null;
        if (bitmap != null) {
            data = new byte[mDataSize];
            int intColor;
            int offset = 0;
            for (int y = 0; y < mHeight; y++) {
                for (int x = 0; x < mWidth; x++) {
                    intColor = bitmap.getPixel(x, y);
                    data[offset++] = (byte) ((intColor >> 16) & 0xFF);
                    data[offset++] = (byte) ((intColor >> 8) & 0xFF);
                    data[offset++] = (byte) ((intColor >> 0) & 0xFF);
                }
            }
        }
        return data;
    }

My new implementation (in android) uses Filter [/DCTDecode /FlateDecode]

private byte[] getBitmapData(Bitmap bitmap) {
    if (bitmap != null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
        bitmap.recycle();
        return stream.toByteArray();
    }
    return null;
}

Here's the source code for this class

https://github.com/Turbo87/apwlibrary/blob/master/apwlibrary/src/main/java/crl/android/pdfwriter/XObjectImage.java

But my approach fails and output pdf displays a blank white page. As I'm using DCT Decode i'm quite not sure if toByteArray() returns the data suitable for DCTDecode.

How can I extract data from compressed jpeg file and add to pdf, to be later decoded by DCTDecode and shown in pdf?

I'm inexperienced in pdf-generation. Help is appreciated

READ ALSO
How can I attach multiple extras to a single Intent in the Bundle object?

How can I attach multiple extras to a single Intent in the Bundle object?

I'm receiving several notifications each with its own unique id

16
React Native TextField component in external library on focus

React Native TextField component in external library on focus

I have a Register Component that has PhoneInput Component from the external library https://githubcom/thegamenicorus/react-native-phone-input The problem is I want to execute some code as soon as TextField inside the PhoneInput is Focused

36
Installing Flutter on Mac OS

Installing Flutter on Mac OS

on running flutter doctor i get the below dependency lacking :

27
Fingerprint after inactivity

Fingerprint after inactivity

I am implementing fingerprint support to protect sensitive areas of my app and access to some web APIs (using a signature)I am initializing the key generator like this:

26