简体   繁体   English

在 PDF 上缩放位图打印而不会丢失 Android Studio 中的图像质量

[英]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).要求:- 我需要在 PDF 文档(A4 大小的纸张)上打印图像。 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.裁剪:- 如果有必要,我只是使用 UCROP 来裁剪和编辑图像作为选项。

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.但我需要将这些打印在 A4 尺寸的纸张上作为 PDF 文档。 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.但问题是,当我这样做时,PDF 上缩放位图的图像质量非常低,无法正确读取细节。

Screenshot of a Sample PDF示例 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)不是在添加之前缩放位图,而是在以原始分辨率添加位图之前缩放 pdf 画布,这对我来说产生了很好的结果,因为 pdf 将其缩小(PDF 中的原始位图是更高质量的图像)

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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