From d59d55575d913646b7a90fca651904ab889c6723 Mon Sep 17 00:00:00 2001
From: riku <risaku@163.com>
Date: 星期五, 18 二月 2022 16:30:53 +0800
Subject: [PATCH] ### 一、任务模块相关

---
 app/src/main/java/cn/flightfeather/thirdappmodule/module/common/CameraActivity.kt |  264 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 232 insertions(+), 32 deletions(-)

diff --git a/app/src/main/java/cn/flightfeather/thirdappmodule/module/common/CameraActivity.kt b/app/src/main/java/cn/flightfeather/thirdappmodule/module/common/CameraActivity.kt
index f9d6440..699d98d 100644
--- a/app/src/main/java/cn/flightfeather/thirdappmodule/module/common/CameraActivity.kt
+++ b/app/src/main/java/cn/flightfeather/thirdappmodule/module/common/CameraActivity.kt
@@ -1,115 +1,243 @@
 package cn.flightfeather.thirdappmodule.module.common
 
+import android.annotation.SuppressLint
 import android.app.Activity
 import android.content.Intent
 import android.graphics.Bitmap
 import android.graphics.BitmapFactory
+import android.graphics.Canvas
 import android.graphics.Matrix
 import android.media.ExifInterface
 import android.os.Bundle
+import android.os.Environment
 import android.os.Environment.DIRECTORY_PICTURES
 import android.view.View
 import cn.flightfeather.thirdappmodule.R
+import cn.flightfeather.thirdappmodule.activity.PhotoViewerActivity
 import cn.flightfeather.thirdappmodule.module.base.BaseActivity
-import cn.flightfeather.thirdappmodule.module.base.BaseTakePicActivity
-import cn.flightfeather.thirdappmodule.util.DateFormatter
+import cn.flightfeather.thirdappmodule.module.base.VIEW_PHOTO
+import cn.flightfeather.thirdappmodule.util.*
 import cn.flightfeather.thirdappmodule.util.location.LocationUtil
 import cn.flightfeather.thirdappmodule.util.photo.ExifHelper
 import cn.flightfeather.thirdappmodule.util.photo.ImageMergeUtil
+import com.lcw.library.imagepicker.ImagePicker
 import com.otaliastudios.cameraview.CameraListener
 import com.otaliastudios.cameraview.CameraOptions
+import com.otaliastudios.cameraview.Size
 import io.reactivex.Observable
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.schedulers.Schedulers
 import kotlinx.android.synthetic.main.activity_camera.*
 import java.io.File
 import java.io.FileOutputStream
+import java.io.Serializable
 import java.util.*
+import kotlin.collections.ArrayList
+import kotlin.math.abs
+import kotlin.math.round
 
 /**
  *
  */
 class CameraActivity : BaseActivity(), View.OnClickListener {
 
+    // gps瀹氫綅宸ュ叿
     private lateinit var locationUtil: LocationUtil
 
+    // 鍥剧墖鏂囨湰鍚堝苟宸ュ叿
     private lateinit var imageMergeUtil: ImageMergeUtil
 
-    override fun getLayoutId(): Int =R.layout.activity_camera
+    // 鎷嶆憚鍥剧墖缂撳瓨鍦板潃
+    private var photoPath = mutableListOf<String>()
+
+    // 鍥剧墖姘村嵃
+    private lateinit var logo: Bitmap
+
+    // 鍏佽鏈�澶у浘鐗囨媿鎽勬暟閲�
+    private var maxPic = 3
+
+    override fun getLayoutId(): Int = R.layout.activity_camera
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
+        initLogoBitmap()
         initCamera()
         initLocation()
+        img_cancel.setOnClickListener(this)
+        img_confirm.setOnClickListener(this)
+        img_thumbnail.setOnClickListener(this)
+//        toggle(false)
+
+        maxPic = intent.getIntExtra("maxPic", 3)
+
+        onShootDone()
     }
 
     override fun onResume() {
-        camera_view.start()
         super.onResume()
+        camera_view.start()
     }
 
     override fun onPause() {
-        camera_view.stop()
         super.onPause()
+        camera_view.stop()
     }
 
     override fun onDestroy() {
-        locationUtil.onDestroy()
         super.onDestroy()
+        camera_view.destroy()
+        locationUtil.onDestroy()
+    }
+
+    private fun toggle(isShot: Boolean = true) {
+        if (isShot) {
+            cl_part1.visibility = View.GONE
+            cl_part2.visibility = View.VISIBLE
+        } else {
+            cl_part1.visibility = View.VISIBLE
+            cl_part2.visibility = View.GONE
+        }
+    }
+
+    private fun initLogoBitmap() {
+        logo = BitmapFactory.decodeResource(resources, R.mipmap.ic_logo)
     }
 
     private fun initCamera() {
         imageMergeUtil = ImageMergeUtil(this)
+
+        // 鏍规嵁绯荤粺鎻愪緵鐨勫彲閫夊垎杈ㄧ巼锛岄�夋嫨鍜屽綋鍓嶆樉绀哄尯鍩熸渶鎺ヨ繎鐨�
+        camera_view.setPictureSize{
+            var min = -1//褰撳墠鍒嗚鲸鐜囦笌鏈�灏忓垎杈ㄧ巼鐨勯暱瀹藉垎鍒搴旂殑宸�肩殑鍜�
+
+//            val screenWith = ScreenUtils.getScreenWidth(this)
+//            val screenHeight = ScreenUtils.getScreenHeight(this)
+
+            val screenWith = camera_view.width
+            val screenHeight = camera_view.height
+
+            val resultList = mutableListOf<Size>()
+
+            it.forEach {size ->
+                if (size.width > screenWith) return@forEach
+
+                val w = abs(size.width - screenWith)
+                val h = abs(size.height - screenHeight)
+                if (min < 0) {
+                    min = w + h
+                    resultList.add(0, size)
+                }else if (w + h < min) {
+                    min = w + h
+                    resultList.add(0, size)
+                }
+            }
+            resultList
+        }
         camera_view.addCameraListener(object : CameraListener() {
             override fun onCameraOpened(options: CameraOptions) {
                 super.onCameraOpened(options)
-                btn_shot.setOnClickListener(this@CameraActivity)
+                btn_shoot.setOnClickListener(this@CameraActivity)
             }
 
 
             override fun onPictureTaken(jpeg: ByteArray?) {
                 super.onPictureTaken(jpeg)
-                val d = Observable.create<String> {
-                    jpeg?.let {
-                        var b = toBitmap(it)
-                        val x = txt_location.x - camera_view.x
-                        val y = txt_location.y - camera_view.y
-                        b?.let {bitmap ->
-                            b = imageMergeUtil.mergeText(bitmap, txt_location.text.toString(), Pair(x, y))
-                            // todo: 2020/5/31 淇濆瓨鍥剧墖
-//                            SystemServiceUtils.saveBitmap(0)
-                        }
+                camera_view.stop()
+                jpeg?.let {
+                    toBitmap(it)?.let {b->
+                        merge(b)
                     }
                 }
-                val pathList = arrayListOf<String>()
-                val intent = Intent()
-                intent.putStringArrayListExtra(BaseTakePicActivity.EXTRA_SELECT_IMAGES, pathList)
-                setResult(Activity.RESULT_OK, intent)
-                onBackPressed()
             }
 
             override fun onCameraClosed() {
                 super.onCameraClosed()
-                onBackPressed()
             }
         })
     }
 
+    @SuppressLint("SetTextI18n")
     private fun initLocation() {
         locationUtil = LocationUtil(this)
         locationUtil.startLocation {
             val date = Date()
             val time = DateFormatter.dateTimeFormat3.format(date)
-            val locationInfo = "缁忓害: ${it.longitude}" +
-                    "\n绾害: ${it.latitude}" +
-                    "\n鍦板潃: ${it.address}" +
-                    "\n鏃堕棿: $time"
             runOnUiThread {
-                txt_location.text = locationInfo
+                txt_latitude.text = "缁忓害: ${round(it.longitude * 1000000) / 1000000}"
+                txt_longitude.text = "绾害: ${round(it.latitude * 1000000) / 1000000}"
+                txt_address.text = "鍦板潃: ${it.address}"
+                txt_time.text = "鏃堕棿: $time"
             }
         }
     }
 
-    override fun onClick(v: View?) {
-        camera_view.capturePicture()
+    private fun merge(b: Bitmap) {
+        val d = Observable.create<Bitmap> { emitter ->
+            var newB: Bitmap
+            val width = camera_view.width
+            val height = camera_view.height
+
+            var x = txt_latitude.x - camera_view.x
+            var y = txt_latitude.y - camera_view.y
+            var xScale = x / width
+            var yScale = y / height
+            newB = imageMergeUtil.mergeText(b, txt_latitude, Pair(x, y), xScale, yScale)
+
+            x = txt_longitude.x - camera_view.x
+            y = txt_longitude.y - camera_view.y
+            xScale = x / width
+            yScale = y / height
+            newB = imageMergeUtil.mergeText(newB, txt_longitude, Pair(x, y), xScale, yScale)
+
+            x = txt_address.x - camera_view.x
+            y = txt_address.y - camera_view.y
+            xScale = x / width
+            yScale = y / height
+            newB = imageMergeUtil.mergeText(newB, txt_address, Pair(x, y), xScale, yScale)
+
+            x = txt_time.x - camera_view.x
+            y = txt_time.y - camera_view.y
+            xScale = x / width
+            yScale = y / height
+            newB = imageMergeUtil.mergeText(newB, txt_time, Pair(x, y), xScale, yScale)
+
+            newB = imageMergeUtil.addLogo(newB, logo)
+
+            // 缂撳瓨鍥剧墖
+            val path = Environment.getExternalStorageDirectory().path + "/FlightFeather/Temp/" + UUIDGenerator.generateUUID(4) + ".jpg"
+            photoPath.add(path)
+            SystemServiceUtils.saveBitmap(path, newB)
+
+            emitter.onNext(newB)
+            emitter.onComplete()
+        }.subscribeOn(Schedulers.io())
+            .observeOn(AndroidSchedulers.mainThread())
+            .subscribe {
+//                toggle(true)
+
+                // 鎷嶆憚loading缁撴潫
+                onShootDone()
+
+                // 鍦ㄧ缉鐣ュ浘涓婃樉绀烘渶鏂颁竴寮犲浘鐗�, 骞舵洿鏂扮‘璁ゆ寜閽枃瀛楁樉绀�
+                img_thumbnail.setImageBitmap(it)
+                val t = "${getString(R.string.yes)}(${photoPath.size}/${maxPic})"
+                img_confirm.text = t
+                // 鍒ゆ柇鏄惁杈惧埌鍏佽鏈�澶у浘鐗囨暟閲�
+                if (photoPath.size == maxPic) {
+                    // 寮瑰嚭鎻愮ず妗嗭紝纭鎴栬�呴噸鎷�
+                    DialogUtil2.showAlertDialog2(this, "鏈�澶氭媿${maxPic}寮犵収鐗�", getString(R.string.yes), getString(R.string.reshoot), { d ->
+                        d.dismiss()
+                        onConfirm()
+                    }, { d ->
+                        d.dismiss()
+                        onCancel()
+                        camera_view.start()
+                    })
+                } else {
+                    camera_view.start()
+                }
+            }
+        disposableList.add(d)
     }
 
     private fun toBitmap(source: ByteArray): Bitmap? {
@@ -123,7 +251,7 @@
             outputStream.write(source)
             val exif = ExifInterface(file.absolutePath)
             val exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
-                    ExifInterface.ORIENTATION_NORMAL)
+                ExifInterface.ORIENTATION_NORMAL)
             orientation = ExifHelper.getOrientation(exifOrientation)
             flip = exifOrientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL ||
                     exifOrientation == ExifInterface.ORIENTATION_FLIP_VERTICAL ||
@@ -146,7 +274,7 @@
                 matrix.setRotate(orientation.toFloat())
                 val temp = bitmap
                 bitmap = Bitmap.createBitmap(bitmap!!, 0, 0, bitmap.width,
-                        bitmap.height, matrix, true)
+                    bitmap.height, matrix, true)
                 temp!!.recycle()
             }
         } catch (e: OutOfMemoryError) {
@@ -154,4 +282,76 @@
         }
         return bitmap
     }
+
+    /**
+     * 纭杩斿洖
+     */
+    private fun onConfirm() {
+        if (photoPath.isEmpty()) return
+        val intent = Intent()
+        intent.putStringArrayListExtra(ImagePicker.EXTRA_SELECT_IMAGES, ArrayList(photoPath))
+        setResult(Activity.RESULT_OK, intent)
+        this.finish()
+    }
+
+    /**
+     * 鍙栨秷鎷嶆憚
+     */
+    private fun onCancel() {
+        photoPath.forEach {
+            val pic = File(it)
+            if (pic.exists()) pic.delete()
+        }
+        photoPath.clear()
+        img_thumbnail.setImageResource(R.drawable.bg_btngroup)
+        img_confirm.text = getString(R.string.yes)
+    }
+
+    /**
+     * 鐓х墖鎷嶆憚澶勭悊涓�
+     */
+    private fun onShooting() {
+        pro_shooting.visibility = View.VISIBLE
+        btn_shoot.isEnabled = false
+    }
+
+    /**
+     * 鐓х墖鎷嶆憚澶勭悊瀹屾瘯
+     */
+    private fun onShootDone() {
+        pro_shooting.visibility = View.GONE
+        btn_shoot.isEnabled = true
+    }
+
+    override fun onBackPressed() {
+        onCancel()
+        super.onBackPressed()
+    }
+
+    override fun onClick(v: View?) {
+        when (v?.id) {
+            R.id.btn_shoot -> {
+                onShooting()
+                camera_view.capturePicture()
+//                capture()
+            }
+            // 鍙栨秷鎷嶆憚鐨勫浘鐗囧苟鍥為��
+            R.id.img_cancel -> {
+                onBackPressed()
+            }
+            //
+            R.id.img_confirm -> onConfirm()
+            R.id.img_thumbnail -> {
+                if (photoPath.isEmpty()) return
+                val fileList = mutableListOf<File>()
+                photoPath.forEach { fileList.add(File(it)) }
+                val intent = Intent(this, PhotoViewerActivity::class.java)
+                intent.putExtra("position", fileList.size - 1)
+                intent.putExtra("type", PhotoViewerActivity.EVIDENCE_PHOTO_TEMP)
+                intent.putExtra("deletable", false)
+                intent.putExtra(PhotoViewerActivity.PARA_FILES, fileList as Serializable)
+                startActivityForResult(intent, VIEW_PHOTO)
+            }
+        }
+    }
 }

--
Gitblit v1.9.3