簡體   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