繁体   English   中英

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

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

要求:- 我需要在 PDF 文档(A4 大小的纸张)上打印图像。 来源:-我通过相机和画廊获得了图像。 裁剪:- 如果有必要,我只是使用 UCROP 来裁剪和编辑图像作为选项。

问题:-我可以做上面提到的所有事情而没有任何错误,结果也很好。 但我需要将这些打印在 A4 尺寸的纸张上作为 PDF 文档。 为此,我必须缩小这些位图以匹配所需的大小。 但问题是,当我这样做时,PDF 上缩放位图的图像质量非常低,无法正确读取细节。

示例 PDF 的屏幕截图

请帮助我在这个问题上取得成功。 您的友好回复是非常受欢迎和高度赞赏的。

//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;
}

不是在添加之前缩放位图,而是在以原始分辨率添加位图之前缩放 pdf 画布,这对我来说产生了很好的结果,因为 pdf 将其缩小(PDF 中的原始位图是更高质量的图像)

以下是适合使用我使用的方法的代码。

//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