ychk il y a 1 mois
Parent
commit
f57b37849a

+ 39 - 137
app/src/main/java/com/tofly/psjgsn/base/TakePhotoActivity.java

@@ -26,7 +26,6 @@ import androidx.annotation.Nullable;
 import androidx.appcompat.app.AlertDialog;
 
 import com.jph.takephoto.app.TakePhoto;
-import com.jph.takephoto.app.TakePhotoImpl;
 import com.jph.takephoto.compress.CompressConfig;
 import com.jph.takephoto.model.InvokeParam;
 import com.jph.takephoto.model.TContextWrap;
@@ -34,10 +33,8 @@ import com.jph.takephoto.model.TImage;
 import com.jph.takephoto.model.TResult;
 import com.jph.takephoto.permission.InvokeListener;
 import com.jph.takephoto.permission.PermissionManager;
-import com.jph.takephoto.permission.TakePhotoInvocationHandler;
 import com.tofly.latte_core.base.LatteActivity;
 import com.tofly.psjgsn.R;
-import com.tofly.psjgsn.entity.UserBean;
 import com.tofly.psjgsn.route.util.AmapUtils.AmapLocationHelper;
 import com.tofly.psjgsn.route.util.Location.LocationUtil;
 import com.tofly.psjgsn.route.util.Location.LocationUtil_xu;
@@ -46,12 +43,10 @@ import com.tofly.psjgsn.utils.DateUtils;
 import com.tofly.psjgsn.utils.ToastUtil;
 
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.Objects;
 
-import cn.hzw.graffiti.WaterMarkParams;
 import permissions.dispatcher.NeedsPermission;
 import permissions.dispatcher.OnShowRationale;
 
@@ -61,62 +56,49 @@ import permissions.dispatcher.OnShowRationale;
  * Description: 处理用户拍照的页面,需要的时候可以继承
  */
 public abstract class TakePhotoActivity extends LatteActivity implements TakePhoto.TakeResultListener, InvokeListener {
+
     public static final int AUDIO_REQUEST_CODE = 19;
     public static final int VIDEO_REQUEST_CODE = 66;
-    private static String yhbh = null;
     protected TakePhoto takePhoto;
     private InvokeParam invokeParam;
     private AmapLocationHelper amapLocationHelper;
     private LocationUtil_xu.CoordinateClass lo = null;
+    private String yhbh = null;
 
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         if (takePhoto == null) {
-            takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
+            takePhoto = getTakePhoto();
         }
-
-        //takePhoto=getTakePhoto();
         takePhoto.onCreate(savedInstanceState);
         takePhoto.onEnableCompress(getCompressConfig(), true);
-
-
         amapLocationHelper = new AmapLocationHelper();
         amapLocationHelper.startLocating();
-
         amapLocationHelper.setMapLocationListener((location, addr, ws84) -> {
             if (TextUtils.isEmpty(addr)) {
                 amapLocationHelper.resetLocation();
             }
             lo = new LocationUtil_xu.CoordinateClass(ws84.longitude, ws84.latitude, addr);
         });
-
     }
 
     /**
      * 获取TakePhoto实例
-     *
      */
-    public TakePhoto getTakePhoto() {
+    private TakePhoto getTakePhoto() {
         if (takePhoto == null) {
-            takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
+            takePhoto = getTakePhoto();
         }
         return takePhoto;
     }
 
-    public CompressConfig getCompressConfig() {
+    private CompressConfig getCompressConfig() {
         //设置压缩参数
         //压缩参数
         return new CompressConfig.Builder().setMaxSize(50 * 1024).setMaxPixel(800).create();
     }
 
-
-    public void setYHBH(String yhbhStr) {
-        yhbh = yhbhStr;
-    }
-
-    //public abstract TakePhoto getTakePhoto();
-
     /**
      * 打开文件夹
      */
@@ -133,8 +115,8 @@ public abstract class TakePhotoActivity extends LatteActivity implements TakePho
 
     @Override
     protected void onSaveInstanceState(Bundle outState) {
+        getTakePhoto().onSaveInstanceState(outState);
         super.onSaveInstanceState(outState);
-        takePhoto.onSaveInstanceState(outState);
     }
 
     @Override
@@ -157,8 +139,8 @@ public abstract class TakePhotoActivity extends LatteActivity implements TakePho
 
     @Override
     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
+        getTakePhoto().onActivityResult(requestCode, resultCode, data);
         super.onActivityResult(requestCode, resultCode, data);
-        takePhoto.onActivityResult(requestCode, resultCode, data);
     }
 
     public abstract void onTakePhotoSuccess(String filePath);
@@ -176,40 +158,15 @@ public abstract class TakePhotoActivity extends LatteActivity implements TakePho
         try {
             //获取照片成功
             String originalPath = result.getImage().getOriginalPath();
-
             boolean isPhotoWaterMark = MyApp.getInstance().getIsPhotoWatermark() == 1;
-
             if (isPhotoWaterMark) {
                 //通过路径获取图片,将图片转换成bitmap.在通过对bitmap进行添加水印
                 Bitmap bitmap = BitmapFactory.decodeFile(originalPath);
-                WaterMarkParams waterMarkParams = getWaterMarkParams();
-                String fistLine = "经度:" + "" + " 纬度:";
-                String secLine = "地址:";
-                if (waterMarkParams != null) {
-                    fistLine = "经度:" + waterMarkParams.mLongitude + " 纬度:" + waterMarkParams.mLatitude;
-                    secLine = "地址:" + waterMarkParams.mAddress;
-                }
-
-
-                String thirdLine = "上报时间:" + DateUtils.getCurrentDate();
-                String mark;
-
-                if (yhbh != null) {
-                    mark = fistLine + "\n" + secLine + "\n" + thirdLine + "\n" + "用户编号: " + yhbh;
-                } else {
-                    mark = fistLine + "\n" + secLine + "\n" + thirdLine;
-                }
-
-                Bitmap watermark = createWatermark(bitmap, mark);
-
+                Bitmap watermark = createWatermark(bitmap);
                 File file = saveImage(watermark);
-
                 String absolutePath = file.getAbsolutePath();
                 TImage tImage = result.getImage();
                 tImage.setOriginalPath(absolutePath);
-
-                //删除原图
-                //new File(originalPath).delete();
             }
             //重新覆盖保存并返回
             String imgUrl = result.getImage().getOriginalPath();
@@ -220,7 +177,6 @@ public abstract class TakePhotoActivity extends LatteActivity implements TakePho
         }
     }
 
-
     @SuppressLint("IntentReset")
     @NeedsPermission(Manifest.permission.SEND_SMS)
     void sendMessage(String phoneNumber) {
@@ -251,8 +207,7 @@ public abstract class TakePhotoActivity extends LatteActivity implements TakePho
         startActivity(intent);
     }
 
-
-    public static File saveImage(Bitmap bmp) {
+    private File saveImage(Bitmap bmp) {
         File appDir = new File(Environment.getExternalStorageDirectory(), "/sn_route/image/temp/");
         if (!appDir.exists()) {
             appDir.mkdir();
@@ -263,130 +218,80 @@ public abstract class TakePhotoActivity extends LatteActivity implements TakePho
         } else {
             fileName = System.currentTimeMillis() + ".jpg";
         }
-
         File file = new File(appDir, fileName);
         try {
             FileOutputStream fos = new FileOutputStream(file);
             bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
             fos.flush();
             fos.close();
-
-        } catch (FileNotFoundException e) {
-            e.printStackTrace();
+            bmp.recycle();
         } catch (IOException e) {
             e.printStackTrace();
         }
-
         return file;
     }
 
-    protected WaterMarkParams getWaterMarkParams() {
-        WaterMarkParams params = null;
+    private String getTextWaterMark() {
+        String markText;
+        String fistLine = "经度:" + "    " + " 纬度:";
+        String secLine = "地址:";
         if (lo != null) {
-            params = new WaterMarkParams();
-            params.mbNeedGps = 1;
             String strLng = LocationUtil.formatLocation(lo.Longtitude);
             String strLat = LocationUtil.formatLocation(lo.Latitude);
-            if (!strLng.isEmpty() && !strLat.isEmpty()) {
-                double lng = Double.parseDouble(strLng);
-                double lat = Double.parseDouble(strLat);
-                strLng = LocationUtil.formatLocation(lng);
-                strLat = LocationUtil.formatLocation(lat);
-                params.mLongitude = strLng;
-                params.mLatitude = strLat;
+            if (!TextUtils.isEmpty(strLng) && !TextUtils.isEmpty(strLat)) {
+                fistLine = "经度:" + strLng + "    纬度:" + strLat;
             }
-            params.mAddress = lo.addr;
-            UserBean userBean = new UserBean();
-            if (userBean != null) {
-                params.mUser = userBean.getUsername();
-                params.mCompany = userBean.getCompany_name();
+            if (!TextUtils.isEmpty(lo.addr)) {
+                secLine = "地址:" + lo.addr;
             }
-            params.mReportTime = DateUtils.getDate();
         }
-        return params;
+        String thirdLine = "上报时间:" + DateUtils.getCurrentDate();
+        if (yhbh != null) {
+            markText = fistLine + "\n" + secLine + "\n" + thirdLine + "\n" + "用户编号: " + yhbh;
+        } else {
+            markText = fistLine + "\n" + secLine + "\n" + thirdLine;
+        }
+        return markText;
     }
 
-    public Bitmap createWatermark(Bitmap bitmapS, String markText) {
-        Bitmap bitmap = bitmapS;
-
-        // 获取图片的宽高
-        int bitmapWidth = bitmap.getWidth();
-
-        int bitmapHeight = bitmap.getHeight();
-
-        // 创建一个和图片一样大的背景图
+    private Bitmap createWatermark(Bitmap bitmapS) {
+        String markText = getTextWaterMark();
+        int bitmapWidth = bitmapS.getWidth();
+        int bitmapHeight = bitmapS.getHeight();
         Bitmap bmp = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
-
         Canvas canvas = new Canvas(bmp);
-
-        // 画背景图
-        canvas.drawBitmap(bitmap, 0, 0, null);
-
+        canvas.drawBitmap(bitmapS, 0, 0, null);
         //-------------开始绘制文字-------------------------------
         if (!TextUtils.isEmpty(markText)) {
             int screenWidth = getScreenWidth();
-
-            float textSize = dp2px(this, 10) * bitmapWidth / screenWidth;
-
-            // 创建画笔
+            float textSize = (float) (dp2px(this, 12) * bitmapWidth) / screenWidth;
             TextPaint mPaint = new TextPaint();
-
-            // 文字矩阵区域
             Rect textBounds = new Rect();
-
-            // 水印的字体大小
             mPaint.setTextSize(textSize);
-
-            // 文字阴影
-            mPaint.setShadowLayer(0.5f, 0f, 1f, Color.YELLOW);
-
-            // 抗锯齿
+            mPaint.setShadowLayer(0.5f, 0f, 1f, Color.WHITE);
             mPaint.setAntiAlias(true);
-
-            // 水印的区域
             mPaint.getTextBounds(markText, 0, markText.length(), textBounds);
-
-            // 水印的颜色
             mPaint.setColor(Color.WHITE);
-
-            StaticLayout layout = new StaticLayout(markText, 0, markText.length(), mPaint, (int) (bitmapWidth - textSize),
-
-                    Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.5F, true);
-
-            // 文字开始的坐标
-            float textX = bitmapWidth * 1 / 40;
-
-            float textY = (bitmapHeight * 6) / 7;//图片的中间
-
-            // 画文字
+            StaticLayout layout = new StaticLayout(markText, 0, markText.length(), mPaint, (int) (bitmapWidth - dp2px(this, 80)),
+                    Layout.Alignment.ALIGN_NORMAL, 1.1f, 0.5f, true);
+            float textX = (float) dp2px(this, 40);
+            float textY = (float) (bitmapHeight - dp2px(this, 240));//图片的中间
             canvas.translate(textX, textY);
-
             layout.draw(canvas);
-
         }
-
-        //保存所有元素
         canvas.save();
-
         canvas.restore();
-
+        bitmapS.recycle();
         return bmp;
-
     }
 
-
     private int getScreenWidth() {
         DisplayMetrics dm = new DisplayMetrics();
-
         WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
-
         wm.getDefaultDisplay().getMetrics(dm);
-
         return dm.widthPixels;
-
     }
 
-
     @Override
     public void takeFail(TResult result, String msg) {
         onTakePhotoFail(result, msg);
@@ -398,10 +303,8 @@ public abstract class TakePhotoActivity extends LatteActivity implements TakePho
         onTakePhotoCancel();
     }
 
-
     public Uri getImageUrl(String yhbh) {
-
-        TakePhotoActivity.yhbh = yhbh;
+        this.yhbh = yhbh;
         File file = new File(Environment.getExternalStorageDirectory(), "/sn_route/image/temp/" + System.currentTimeMillis() + "_" + yhbh + ".jpg");
         if (!Objects.requireNonNull(file.getParentFile()).exists()) {
             file.getParentFile().mkdirs();
@@ -417,7 +320,6 @@ public abstract class TakePhotoActivity extends LatteActivity implements TakePho
         return Uri.fromFile(file);
     }
 
-
     @Override
     protected void onDestroy() {
         super.onDestroy();

+ 1 - 1
app/src/main/java/com/tofly/psjgsn/utils/BitmapUtil.java

@@ -139,7 +139,7 @@ public class BitmapUtil {
     /**
      * 回收bitmap
      */
-    public static void recycleBitmap(Bitmap bitmap) {
+    private static void recycleBitmap(Bitmap bitmap) {
         if (bitmap != null && !bitmap.isRecycled()) {
             bitmap.recycle();
         }