Skip to content

Export in JPEG #1588

@guillaume-lecomte

Description

@guillaume-lecomte

Feature Request

For one of my projects, it was needed to use a bitmap compress to JPEG. I would like to be able to choose the compress format in the toDataURL.

Why it is needed

The picture I use is too big, I would like to be able to compress in JPEG.

Possible implementation

We could add a new parameter to the toDataURL function. It could be a string param like 'PNG' or 'JPEG'

Code sample

    String toDataURL(int width, int height, string compressionFormat) {
        Bitmap bitmap = Bitmap.createBitmap(
                width,
                height,
                Bitmap.Config.ARGB_8888);

        clearChildCache();
        drawChildren(new Canvas(bitmap));
        clearChildCache();
        this.invalidate();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        if(compressionFormat == "jpeg") {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        }else {
           bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        }
      
        bitmap.recycle();
        byte[] bitmapBytes = stream.toByteArray();
        return Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions