Browse Source

pdftools(android) - 图片透明度,旋转的图片注释问题修复

libai 1 year ago
parent
commit
560013a2cb

+ 37 - 2
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/utils/annotation/CPDFAnnotationManager.java

@@ -13,8 +13,11 @@ package com.compdfkit.tools.common.utils.annotation;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
+import android.graphics.Matrix;
 import android.graphics.PointF;
 import android.graphics.RectF;
+import android.media.ExifInterface;
+import android.util.Log;
 
 import androidx.fragment.app.FragmentActivity;
 import androidx.fragment.app.FragmentManager;
@@ -147,13 +150,35 @@ public class CPDFAnnotationManager {
         CPDFPage page = readerView.getPDFDocument().pageAtIndex(readerView.getPageNum());
         CPDFStampAnnotation stampAnnotation = (CPDFStampAnnotation) page.addAnnot(CPDFAnnotation.Type.STAMP);
         stampAnnotation.setImageStamp(imagePath);
+
+        ExifInterface exif = null;
+        int rotate = 0;
+        try {
+            exif = new ExifInterface(imagePath);
+            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
+            switch (orientation) {
+                case ExifInterface.ORIENTATION_ROTATE_90:
+                    rotate = 90;
+                    break;
+                case ExifInterface.ORIENTATION_ROTATE_180:
+                    rotate = 180;
+                    break;
+                case ExifInterface.ORIENTATION_ROTATE_270:
+                    rotate = 270;
+                    break;
+            }
+        } catch (Exception e) {
+
+        }
+
+
         float defaultWidth = 200F;
         PointF vertex = new PointF(pointF.x - (defaultWidth / 2), pointF.y);
         BitmapFactory.Options options = new BitmapFactory.Options();
         options.inJustDecodeBounds = true;
         BitmapFactory.decodeFile(imagePath, options);
         RectF insertRect = new RectF(vertex.x, vertex.y, vertex.x + defaultWidth, vertex.y
-                + defaultWidth * options.outHeight / options.outWidth);
+                + defaultWidth * ((rotate == 0 || rotate == 180) ? options.outHeight / options.outWidth : options.outWidth / options.outHeight));
         RectF pageSize = readerView.getPageNoZoomSize(readerView.getPageNum());
         insertRect.set(page.convertRectToPage(readerView.isCropMode(), pageSize.width(),
                 pageSize.height(), insertRect));
@@ -162,7 +187,17 @@ public class CPDFAnnotationManager {
             BitmapFactory.Options tmpOptions = new BitmapFactory.Options();
             tmpOptions.inMutable = true;
             Bitmap bitmap = BitmapFactory.decodeFile(imagePath, tmpOptions);
-            stampAnnotation.updateApWithBitmap(bitmap);
+            if (rotate == 0) {
+                stampAnnotation.updateApWithBitmap(bitmap);
+            } else {
+                int width = bitmap.getWidth();
+                int height = bitmap.getHeight();
+                Matrix matrix = new Matrix();
+                matrix.setRotate(rotate);
+                Bitmap newBM = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
+                stampAnnotation.updateApWithBitmap(newBM);
+                newBM.recycle();
+            }
             bitmap.recycle();
         } else {
             stampAnnotation.setImageStamp(imagePath);

+ 9 - 0
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/views/pdfproperties/pdfstyle/manager/provider/CEditSelectionsProvider.java

@@ -12,6 +12,8 @@ package com.compdfkit.tools.common.views.pdfproperties.pdfstyle.manager.provider
 import android.text.TextUtils;
 
 import com.compdfkit.core.annotation.CPDFTextAttribute;
+import com.compdfkit.core.edit.CPDFEditArea;
+import com.compdfkit.core.edit.CPDFEditImageArea;
 import com.compdfkit.core.edit.CPDFEditTextArea;
 import com.compdfkit.tools.common.views.pdfproperties.pdfstyle.CAnnotStyle;
 import com.compdfkit.tools.common.views.pdfproperties.pdfstyle.CStyleType;
@@ -184,6 +186,13 @@ public class CEditSelectionsProvider implements CStyleProvider {
             } else {
                 style.setFontType(CPDFTextAttribute.FontNameHelper.FontType.Unknown);
             }
+        } else if (style.getType() == CStyleType.EDIT_IMAGE) {
+            CPDFEditArea editArea = pageView.getCurrentEditArea();
+            if (editArea != null && editArea instanceof CPDFEditImageArea) {
+                style.setOpacity((int)(((CPDFEditImageArea)editArea).getTransparency() * 255));
+            } else {
+                style.setOpacity(255);
+            }
         }
         return style;
     }