简体   繁体   中英

Scaled Bitmap print on PDF without loosing Image Quality in Android Studio

Requirements :- I need to print images on a PDF document (A4 size sheets). Source :- I got the images through the Camera and from the Gallery. Crop :- I just used UCROP for cropping and editing the image if it necessary as an option.

Problem :- I can do everything mentioned above without any error and result also very good. But I need those print on a A4 size sheet as a PDF Document. To do that, I had to scaled down those Bitmaps to match the required sized. But the thing is, when I do that the Image Quality of the Scaled Bitmap on PDF is very low and can't read the details correctly.

Screenshot of a Sample PDF

Please help me to get success on this issue. Your kind reply are mostly welcome and highly appreciated.

//boolean img1_SetImage - used to check Img1 is available or not
//img1_Uri - Uri of Img1
if (img1_SetImage) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inScaled = false;
Bitmap bmp = BitmapFactory.decodeFile(img1_Uri.getPath(), opt);
int[] xyImg = xy(bmp.getWidth(), bmp.getHeight(), 298, 175);

PdfDocument.PageInfo myPageInfo2 =
                    new PdfDocument.PageInfo.Builder(595, 842, 1).create();
PdfDocument.Page myPage2 = myPDFDoc.startPage(myPageInfo2);
Canvas myCanvas2 = myPage2.getCanvas();

Bitmap scaledBmp = Bitmap.createScaledBitmap(bmp, xyImg[0], xyImg[1], false);
myCanvas2.drawBitmap(scaledBmp, xyImg[2], xyImg[3], new Paint(Paint.FILTER_BITMAP_FLAG));
bmp.recycle();
scaledBmp.recycle();
}


private int[] xy(float width, float height, float left, float top) {
    int finalWidth, finalHeight, finalLeft, finalTop;
    float wScale, hScale, scaleFactor;
    wScale = (436 / width);
    hScale = (270 / height);

    if (wScale >= hScale) {
        scaleFactor = hScale;
    } else {
        scaleFactor = wScale;
    }
    finalWidth = (int) (width * scaleFactor);
    finalHeight = (int) (height * scaleFactor);
    finalLeft = (int) (left - (finalWidth / 2));
    finalTop = (int) (top - (finalHeight / 2));

    int[] returnValues = {finalWidth, finalHeight, finalLeft, finalTop};

    return returnValues;
}

Instead of scaling the bitmap before adding I scale the pdf canvas before adding the bitmap at original resolution, this produces good results for me as the pdf scales it down (the original bitmap inside the PDF is the higher quality image)

Below is your code adapted to use the method I use.

//boolean img1_SetImage - used to check Img1 is available or not
//img1_Uri - Uri of Img1
if (img1_SetImage) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inScaled = false;
Bitmap bmp = BitmapFactory.decodeFile(img1_Uri.getPath(), opt);

PdfDocument.PageInfo myPageInfo2 =
                    new PdfDocument.PageInfo.Builder(595, 842, 1).create();
PdfDocument.Page myPage2 = myPDFDoc.startPage(myPageInfo2);
Canvas myCanvas2 = myPage2.getCanvas();

// Work out scaleFactor to get all the image on the page
float wScale, hScale, scaleFactor;
wScale = (float) 595 / bmp.getWidth(); // If you don't cast Int/Int = Int so you loose any decimal places.
hScale = (float) 842 / bmp.getHeight();  // Alternative is to define the size as float e.g. 842.0f
if (wScale >= hScale) {
        scaleFactor = hScale;
    } else {
        scaleFactor = wScale;
    }



Paint paint = new Paint();
paint.setAntiAlias(true);
myCanvas2.scale(scaleFactor, scaleFactor);
myCanvas2.drawBitmap(bmp,0,0,paint);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM