18034727885
2026-05-14 dad2ded3e9e9d2edf55c7ce10e791588b526961f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.example.smart.ui.icondraw
 
import android.app.Activity
import android.graphics.Bitmap
import android.graphics.Color
import android.net.Uri
import android.os.AsyncTask
import android.provider.CalendarContract.CalendarCache.URI
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.bumptech.glide.Glide
import com.example.basecore.BaseBindingActivity
import com.example.basecore.util.ImageUtils
import com.example.basecore.util.showCenterToast
import com.example.basecore.util.sysbar.StatusBarUtils
import com.example.smart.R
import com.example.smart.bean.EntityRef
import com.example.smart.bean.ImageDrawData
import com.example.smart.bean.ImageRef
import com.example.smart.databinding.ActivityDrawBinding
import com.example.smart.image.ImageProvider
import com.example.smart.ui.icondraw.DrawView.Companion.shape_rect
import com.example.smart.ui.item.bean.ImageBean
import com.example.smart.widget.dialog.LoadingDialog
import com.github.piasy.biv.BigImageViewer
import com.github.piasy.biv.loader.glide.GlideImageLoader
import com.google.gson.internal.bind.TypeAdapters.URI
import ja.burhanrashid52.photoeditor.PhotoEditor
import ja.burhanrashid52.photoeditor.PhotoEditorView
import ja.burhanrashid52.photoeditor.shape.ShapeBuilder
import ja.burhanrashid52.photoeditor.shape.ShapeType
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.File
import java.net.URI
 
 
class DrawActivity : BaseBindingActivity() {
 
    private lateinit var mImageProvider: ImageProvider
 
    private lateinit var photoEditor: PhotoEditor
    private lateinit var photoEditorView: PhotoEditorView
 
    private lateinit var path: String
    private lateinit var id: String
 
    private inner class ImageCallBack : ImageProvider.IImageCallback {
 
        override fun onUploadSuccess(entityRef: List<ImageRef>) {
            entityRef.forEach {
                //此处path 被使用为id
                DrawLiveData.onDataChange(ImageDrawData(it.id, id, it.name))
                finish()
            }
        }
 
        override fun onUploadFailed() {
            mContext.showCenterToast("图片上传失败,请稍后再试...")
        }
    }
 
    private val binding by lazy {
        ActivityDrawBinding.inflate(LayoutInflater.from(mContext))
    }
 
    override fun getRootView(): View = binding.root
 
    override fun initView() {
        StatusBarUtils.transparentMode(this, true, isFullScreen = true)
 
        BigImageViewer.initialize(GlideImageLoader.with(this))
 
        mImageProvider = ImageProvider(mContext as Activity, ImageCallBack())
        photoEditorView = findViewById(R.id.photoEditorView)
 
        photoEditor = PhotoEditor.Builder(this, photoEditorView)
            .setPinchTextScalable(true)
            .setClipSourceImage(false)
            .build()
 
        path = intent.getStringExtra("path") ?: ""
        id = intent.getStringExtra("id") ?: ""
 
        Glide.with(mContext).load(path).into(photoEditorView.source)
 
        // TODO 编辑图片之后,图片的大小会变小,无法保持原始尺寸。这是PhotoEditor的问题,暂时无法解决
        // 测试用
//        photoEditorView.source.setImageResource(R.drawable.shape_exclude_bg)
 
        with(binding) {
            closeImg.setOnClickListener { finish() }
 
            btnBrush.setOnClickListener {
                photoEditor.setBrushDrawingMode(true)
                photoEditor.brushColor = Color.RED
            }
 
            btnRect.setOnClickListener {
                photoEditor.setBrushDrawingMode(true)
                photoEditor.setShape(ShapeBuilder()
                    .withShapeType(ShapeType.Rectangle)
                    .withShapeColor(Color.RED)
                    .withShapeSize(20f))
            }
 
            btnOral.setOnClickListener {
                photoEditor.setBrushDrawingMode(true)
                photoEditor.setShape(ShapeBuilder()
                    .withShapeType(ShapeType.Oval)
                    .withShapeColor(Color.RED)
                    .withShapeSize(20f))
            }
 
            btnEraser.setOnClickListener {
                photoEditor.brushEraser()
                photoEditor.setBrushEraserSize(100f)
            }
 
            keepTv.setOnClickListener {
                if(photoEditor.isCacheEmpty)
                    return@setOnClickListener
                keepTv.isEnabled = false
                keepTv.setTextColor(Color.GRAY)
                // save as tmp file
                // get a tmp file path
                val file = File(externalCacheDir, "edited_image.png")
                photoEditor.saveAsFile(file.absolutePath, object : PhotoEditor.OnSaveListener {
                    override fun onSuccess(imagePath: String) {
                        // upload tmp file
                        if (::mImageProvider.isInitialized) {
                            mImageProvider.uploadPicAfterDraw(Uri.parse(imagePath))
                        }
                        keepTv.setTextColor(Color.WHITE)
                        keepTv.isEnabled = true
                    }
 
                    override fun onFailure(exception: Exception) {
                        mContext.showCenterToast("图片保存失败,请稍后再试...")
                        keepTv.setTextColor(Color.WHITE)
                        keepTv.isEnabled = true
                    }
                })
 
            }
        }
    }
 
 
    override fun bindDataObserver() {
 
    }
 
 
}
 
 
// AsyncTask to download image in the background
private class DownloadImageTask(
    private val url: String,
    private val externalCacheDir: File,
    private val callback: (String) -> Unit
) : AsyncTask<Void, Void, String>() {
 
    override fun doInBackground(vararg params: Void?): String? {
        val file = File(externalCacheDir, "tmp_image.png")
        return try {
            val response = OkHttpClient().newCall(Request.Builder().url(url).build()).execute()
            response.body?.byteStream()?.use { input ->
                file.outputStream().use { output ->
                    input.copyTo(output)
                }
            }
            file.absolutePath
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }
 
    override fun onPostExecute(result: String?) {
        result?.let { callback(it) }
    }
}