1

I am trying to convert image shot using cameraa to A4 PDF

    // Read image width and height
    float imageWidth = bitmap.getWidth();
    float imageHeight = bitmap.getHeight();

    // Read page width and height ignoring the margins
    float pageWidth = PDRectangle.A4.getWidth() - (margin_left_right * 2);
    float pageHeight = PDRectangle.A4.getHeight() - (margin_top_bottom * 2);

    Log.d("Image Resize", String.valueOf(imageWidth) + " x " + String.valueOf(imageHeight));
    Log.d("Image Resize", String.valueOf(pageWidth) + " x " + String.valueOf(pageHeight));

    # Image: 4160.0 x 3120.0
    # Page: 575.27563 x 821.8898

    Log.d("Image Resize", String.valueOf(requiredRatio));
    Log.d("Image Resize", String.valueOf(newImageWidth) + " x " + String.valueOf(newImageHeight));

    # Required ratio: 0.13828741
    # After scaling: 575 x 431

Problem is image with size 4160 x 3120, after getting resized to 575 x 431, looks very pixelated.

How do PDFs solve this problem in general? Is there a way to say 4160 x 3120 at 600 dpi when writing using contentStream.drawImage()?


This question also supports the fact that if written without resizing on pages of size equal to the height and width of the image, OP could only see the images at 15%, how should that be scaled down without loss of quality?

Saravanabalagi Ramachandran
  • 8,551
  • 11
  • 53
  • 102

1 Answers1

1

There is an overloaded drawImage() method to do that

contentStream.drawImage(PDImageXObject, float x, float y)
contentStream.drawImage(PDImageXObject, float x, float y, float height, float width)

By specifying explicit width and height on contentStream.drawImage(), this can be achieved. For instance if you want to show the image 4 times smaller but still retain the same resolution, it can done using

scaleDownRatio = 0.25;
pageSize = PDRectangle.A4;

# this will draw image at bottom left corner
PDImageXObject pageImageXObject = LosslessFactory.createFromImage(document, bitmap);
contentStream.drawImage(pageImageXObject, 0, 0, bitmap.getWidth() * ratio, bitmap.getHeight() * ratio)

# to center it you can use something like
contentStream.drawImage(pageImageXObject, (pageSize.getWidth() - bitmap.getWidth() * ratio) / 2, (pageSize.getHeight() - bitmap.getHeight() * ratio) / 2, bitmap.getWidth() * ratio, bitmap.getHeight() * ratio)
Saravanabalagi Ramachandran
  • 8,551
  • 11
  • 53
  • 102