Commit 79ce9240dd12aa9c01f45cc7ecdfd56ba6b0889a
1 parent
32434bc9c6
Exists in
master
图片裁剪问题解决
Showing
1 changed file
with
10 additions
and
19 deletions
Show diff stats
app/src/main/java/com/hjx/parent/ImageEditActivity.java
1 | package com.hjx.parent; | 1 | package com.hjx.parent; |
2 | 2 | ||
3 | import android.app.Dialog; | 3 | import android.app.Dialog; |
4 | import android.content.Intent; | 4 | import android.content.Intent; |
5 | import android.graphics.Bitmap; | 5 | import android.graphics.Bitmap; |
6 | import android.graphics.BitmapFactory; | 6 | import android.graphics.BitmapFactory; |
7 | import android.graphics.Rect; | 7 | import android.graphics.Rect; |
8 | import android.hardware.camera2.CameraCharacteristics; | 8 | import android.hardware.camera2.CameraCharacteristics; |
9 | import android.net.Uri; | 9 | import android.net.Uri; |
10 | import android.os.Bundle; | 10 | import android.os.Bundle; |
11 | import android.os.SystemClock; | 11 | import android.os.SystemClock; |
12 | import android.provider.MediaStore; | 12 | import android.provider.MediaStore; |
13 | import android.text.TextUtils; | 13 | import android.text.TextUtils; |
14 | import android.util.Log; | 14 | import android.util.Log; |
15 | import android.view.KeyEvent; | 15 | import android.view.KeyEvent; |
16 | import android.view.MotionEvent; | 16 | import android.view.MotionEvent; |
17 | import android.view.View; | 17 | import android.view.View; |
18 | import android.widget.RelativeLayout; | 18 | import android.widget.RelativeLayout; |
19 | 19 | ||
20 | import androidx.annotation.Nullable; | 20 | import androidx.annotation.Nullable; |
21 | import androidx.core.content.FileProvider; | 21 | import androidx.core.content.FileProvider; |
22 | 22 | ||
23 | import com.hjx.parent.databinding.ActivityImageEditBinding; | 23 | import com.hjx.parent.databinding.ActivityImageEditBinding; |
24 | import com.hjx.parent.dialog.TipDialog; | 24 | import com.hjx.parent.dialog.TipDialog; |
25 | import com.prws.common.utils.BitmapUtils; | 25 | import com.prws.common.utils.BitmapUtils; |
26 | import com.prws.common.utils.CommonUtil; | 26 | import com.prws.common.utils.CommonUtil; |
27 | import com.prws.common.widget.MarkSizeView; | 27 | import com.prws.common.widget.MarkSizeView; |
28 | import com.zhangteng.imagepicker.bean.ImageInfo; | 28 | import com.zhangteng.imagepicker.bean.ImageInfo; |
29 | import com.zhangteng.imagepicker.config.ImagePickerConfig; | 29 | import com.zhangteng.imagepicker.config.ImagePickerConfig; |
30 | import com.zhangteng.imagepicker.config.ImagePickerEnum; | 30 | import com.zhangteng.imagepicker.config.ImagePickerEnum; |
31 | import com.zhangteng.imagepicker.config.ImagePickerOpen; | 31 | import com.zhangteng.imagepicker.config.ImagePickerOpen; |
32 | import com.zhangteng.imagepicker.imageloader.GlideImageLoader; | 32 | import com.zhangteng.imagepicker.imageloader.GlideImageLoader; |
33 | import com.zhangteng.utils.IHandlerCallBack; | 33 | import com.zhangteng.utils.IHandlerCallBack; |
34 | 34 | ||
35 | import java.io.File; | 35 | import java.io.File; |
36 | import java.util.ArrayList; | 36 | import java.util.ArrayList; |
37 | import java.util.List; | 37 | import java.util.List; |
38 | 38 | ||
39 | public class ImageEditActivity extends BaseActivity<ActivityImageEditBinding> { | 39 | public class ImageEditActivity extends BaseActivity<ActivityImageEditBinding> { |
40 | private Bitmap bitmap; | 40 | private Bitmap bitmap; |
41 | private String filepath; | 41 | private String filepath; |
42 | 42 | ||
43 | @Override | 43 | @Override |
44 | public void initView(Bundle savedInstanceState) { | 44 | public void initView(Bundle savedInstanceState) { |
45 | String path = getIntent().getStringExtra("path"); | 45 | String path = getIntent().getStringExtra("path"); |
46 | bitmap = BitmapFactory.decodeFile(path); | 46 | bitmap = BitmapFactory.decodeFile(path); |
47 | showImage(true); | 47 | showImage(true); |
48 | binding.markView.setmOnClickListener(new MarkSizeView.onClickListener() { | 48 | binding.markView.setmOnClickListener(new MarkSizeView.onClickListener() { |
49 | @Override | 49 | @Override |
50 | public void onConfirm(Rect markedArea) { | 50 | public void onConfirm(Rect markedArea) { |
51 | Bitmap backGroundBitmap = binding.markView.getBackGroundBitmap(); | 51 | Bitmap backGroundBitmap = binding.markView.getBackGroundBitmap(); |
52 | int bitmapWidth = backGroundBitmap.getWidth(); | 52 | float sx = 1f * backGroundBitmap.getWidth() / binding.markView.getWidth(); |
53 | int bitmapHeight = backGroundBitmap.getHeight(); | 53 | float sy = 1f * backGroundBitmap.getHeight() / binding.markView.getHeight(); |
54 | int markSizeViewWidth = binding.markView.getWidth(); | 54 | |
55 | int markSizeHeight = binding.markView.getHeight(); | 55 | bitmap = Bitmap.createBitmap( |
56 | Log.d(toString(), markedArea.toString()); | 56 | backGroundBitmap, |
57 | int widthFold = (int) CommonUtil.div(bitmapWidth, markSizeViewWidth, 2); | 57 | (int) (markedArea.left * sx + 0.5), |
58 | int heightFold = (int) CommonUtil.div(bitmapHeight, markSizeHeight, 2); | 58 | (int) (markedArea.top * sy + 0.5), |
59 | 59 | (int) ((markedArea.right - markedArea.left) * sx + 0.5), | |
60 | markedArea.left = CommonUtil.mulForInt(markedArea.left, widthFold); | 60 | (int) ((markedArea.bottom - markedArea.top) * sy + 0.5) |
61 | markedArea.top = CommonUtil.mulForInt(markedArea.top, heightFold); | 61 | ); |
62 | markedArea.right = CommonUtil.mulForInt(markedArea.right, widthFold); | ||
63 | markedArea.bottom = CommonUtil.mulForInt(markedArea.bottom, heightFold); | ||
64 | Log.d(toString(), "markedArea.left=" + markedArea.left + "markedArea.top=" + markedArea.top | ||
65 | + ";markedArea.right=" + markedArea.right + ";markedArea.bottom=" + markedArea.bottom); | ||
66 | bitmap = Bitmap.createBitmap(backGroundBitmap, | ||
67 | markedArea.left, | ||
68 | markedArea.top, | ||
69 | (markedArea.right - markedArea.left) > bitmapWidth ? bitmapWidth : markedArea.right - markedArea.left, | ||
70 | (markedArea.bottom - markedArea.top) > bitmapHeight ? bitmapHeight : markedArea.bottom - markedArea.top); | ||
71 | binding.markView.reset(); | 62 | binding.markView.reset(); |
72 | showImage(false); | 63 | showImage(false); |
73 | } | 64 | } |
74 | 65 | ||
75 | @Override | 66 | @Override |
76 | public void onCancel() { | 67 | public void onCancel() { |
77 | 68 | ||
78 | } | 69 | } |
79 | 70 | ||
80 | @Override | 71 | @Override |
81 | public void onTouch() { | 72 | public void onTouch() { |
82 | 73 | ||
83 | } | 74 | } |
84 | }); | 75 | }); |
85 | binding.llRotate.setOnClickListener(new View.OnClickListener() { | 76 | binding.llRotate.setOnClickListener(new View.OnClickListener() { |
86 | @Override | 77 | @Override |
87 | public void onClick(View v) { | 78 | public void onClick(View v) { |
88 | bitmap = BitmapUtils.routeBitmap(bitmap); | 79 | bitmap = BitmapUtils.routeBitmap(bitmap); |
89 | showImage(false); | 80 | showImage(false); |
90 | } | 81 | } |
91 | }); | 82 | }); |
92 | binding.ivBack.setOnClickListener(new View.OnClickListener() { | 83 | binding.ivBack.setOnClickListener(new View.OnClickListener() { |
93 | @Override | 84 | @Override |
94 | public void onClick(View v) { | 85 | public void onClick(View v) { |
95 | back(); | 86 | back(); |
96 | } | 87 | } |
97 | }); | 88 | }); |
98 | binding.llDelete.setOnClickListener(new View.OnClickListener() { | 89 | binding.llDelete.setOnClickListener(new View.OnClickListener() { |
99 | @Override | 90 | @Override |
100 | public void onClick(View v) { | 91 | public void onClick(View v) { |
101 | setResult(1002); | 92 | setResult(1002); |
102 | finish(); | 93 | finish(); |
103 | } | 94 | } |
104 | }); | 95 | }); |
105 | binding.tvSave.setOnClickListener(new View.OnClickListener() { | 96 | binding.tvSave.setOnClickListener(new View.OnClickListener() { |
106 | @Override | 97 | @Override |
107 | public void onClick(View v) { | 98 | public void onClick(View v) { |
108 | Bitmap b = BitmapUtils.compress(bitmap); | 99 | Bitmap b = BitmapUtils.compress(bitmap); |
109 | String filepath = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; | 100 | String filepath = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; |
110 | CommonUtil.saveBitmapToUri(b, filepath); | 101 | CommonUtil.saveBitmapToUri(b, filepath); |
111 | Intent intent = new Intent(); | 102 | Intent intent = new Intent(); |
112 | intent.putExtra("path", filepath); | 103 | intent.putExtra("path", filepath); |
113 | setResult(1003, intent); | 104 | setResult(1003, intent); |
114 | finish(); | 105 | finish(); |
115 | } | 106 | } |
116 | }); | 107 | }); |
117 | binding.llPhoto.setOnClickListener(new View.OnClickListener() { | 108 | binding.llPhoto.setOnClickListener(new View.OnClickListener() { |
118 | @Override | 109 | @Override |
119 | public void onClick(View v) { | 110 | public void onClick(View v) { |
120 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | 111 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
121 | //确保有相机来处理Intent | 112 | //确保有相机来处理Intent |
122 | filepath = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; | 113 | filepath = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; |
123 | File photoFile = new File(filepath); | 114 | File photoFile = new File(filepath); |
124 | if (photoFile != null) { | 115 | if (photoFile != null) { |
125 | //适配Android 7.0文件权限,通过FileProvider创建一个content类型的Uri | 116 | //适配Android 7.0文件权限,通过FileProvider创建一个content类型的Uri |
126 | Uri photoUri = FileProvider.getUriForFile(activity, "com.hjx.parent.fileprovider", photoFile); | 117 | Uri photoUri = FileProvider.getUriForFile(activity, "com.hjx.parent.fileprovider", photoFile); |
127 | takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); | 118 | takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
128 | takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); | 119 | takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); |
129 | activity.startActivityForResult(takePictureIntent, 1); | 120 | activity.startActivityForResult(takePictureIntent, 1); |
130 | } | 121 | } |
131 | } | 122 | } |
132 | }); | 123 | }); |
133 | } | 124 | } |
134 | 125 | ||
135 | private void back() { | 126 | private void back() { |
136 | showTipDialog("温馨提示", "是否确认放弃当前的编辑?", "取消", "确定", new TipDialog.TipDialogInterface() { | 127 | showTipDialog("温馨提示", "是否确认放弃当前的编辑?", "取消", "确定", new TipDialog.TipDialogInterface() { |
137 | @Override | 128 | @Override |
138 | public void onConfirm(Dialog dialog) { | 129 | public void onConfirm(Dialog dialog) { |
139 | dialog.dismiss(); | 130 | dialog.dismiss(); |
140 | finish(); | 131 | finish(); |
141 | } | 132 | } |
142 | 133 | ||
143 | @Override | 134 | @Override |
144 | public void onCancel(Dialog dialog) { | 135 | public void onCancel(Dialog dialog) { |
145 | dialog.dismiss(); | 136 | dialog.dismiss(); |
146 | } | 137 | } |
147 | }); | 138 | }); |
148 | } | 139 | } |
149 | 140 | ||
150 | @Override | 141 | @Override |
151 | public boolean onKeyDown(int keyCode, KeyEvent event) { | 142 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
152 | if (keyCode == KeyEvent.KEYCODE_BACK) { | 143 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
153 | back(); | 144 | back(); |
154 | return true; | 145 | return true; |
155 | } | 146 | } |
156 | return super.onKeyDown(keyCode, event); | 147 | return super.onKeyDown(keyCode, event); |
157 | } | 148 | } |
158 | 149 | ||
159 | private void showImage(boolean cut) { | 150 | private void showImage(boolean cut) { |
160 | float imageHeight = bitmap.getHeight(); | 151 | float imageHeight = bitmap.getHeight(); |
161 | float imageWidth = bitmap.getWidth(); | 152 | float imageWidth = bitmap.getWidth(); |
162 | float viewHeight = CommonUtil.getScreenHeight(context) - CommonUtil.dpToPx(context, 210); | 153 | float viewHeight = CommonUtil.getScreenHeight(context) - CommonUtil.dpToPx(context, 210); |
163 | float viewWidth = CommonUtil.getScreenWidth(context) - CommonUtil.dpToPx(context, 30); | 154 | float viewWidth = CommonUtil.getScreenWidth(context) - CommonUtil.dpToPx(context, 30); |
164 | binding.markView.post(new Runnable() { | 155 | binding.markView.post(new Runnable() { |
165 | @Override | 156 | @Override |
166 | public void run() { | 157 | public void run() { |
167 | binding.markView.reset(); | 158 | binding.markView.reset(); |
168 | RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(binding.markView.getLayoutParams()); | 159 | RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(binding.markView.getLayoutParams()); |
169 | if (imageHeight / imageWidth > viewHeight / viewWidth) { | 160 | if (imageHeight / imageWidth > viewHeight / viewWidth) { |
170 | if (viewHeight > imageHeight) { | 161 | if (viewHeight > imageHeight) { |
171 | layoutParams.height = (int) imageHeight; | 162 | layoutParams.height = (int) imageHeight; |
172 | layoutParams.width = (int) imageWidth; | 163 | layoutParams.width = (int) imageWidth; |
173 | } else { | 164 | } else { |
174 | layoutParams.height = (int) viewHeight; | 165 | layoutParams.height = (int) viewHeight; |
175 | layoutParams.width = (int) ((float) (imageWidth / imageHeight) * viewHeight); | 166 | layoutParams.width = (int) ((float) (imageWidth / imageHeight) * viewHeight); |
176 | } | 167 | } |
177 | } else { | 168 | } else { |
178 | if (viewWidth > imageWidth) { | 169 | if (viewWidth > imageWidth) { |
179 | layoutParams.height = (int) imageHeight; | 170 | layoutParams.height = (int) imageHeight; |
180 | layoutParams.width = (int) imageWidth; | 171 | layoutParams.width = (int) imageWidth; |
181 | } else { | 172 | } else { |
182 | layoutParams.height = (int) ((float) (imageHeight / imageWidth) * viewWidth); | 173 | layoutParams.height = (int) ((float) (imageHeight / imageWidth) * viewWidth); |
183 | layoutParams.width = (int) viewWidth; | 174 | layoutParams.width = (int) viewWidth; |
184 | } | 175 | } |
185 | } | 176 | } |
186 | layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); | 177 | layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); |
187 | binding.markView.setLayoutParams(layoutParams); | 178 | binding.markView.setLayoutParams(layoutParams); |
188 | binding.markView.setBackGroundBitmap(bitmap); | 179 | binding.markView.setBackGroundBitmap(bitmap); |
189 | binding.markView.reset(); | 180 | binding.markView.reset(); |
190 | if (cut) { | 181 | if (cut) { |
191 | simulateClick(10, layoutParams.width - 10, 10, layoutParams.height - 10); | 182 | simulateClick(10, layoutParams.width - 10, 10, layoutParams.height - 10); |
192 | } | 183 | } |
193 | } | 184 | } |
194 | }); | 185 | }); |
195 | } | 186 | } |
196 | 187 | ||
197 | private void simulateClick(int x1, int x2, int y1, int y2) { | 188 | private void simulateClick(int x1, int x2, int y1, int y2) { |
198 | long downTime = SystemClock.uptimeMillis(); | 189 | long downTime = SystemClock.uptimeMillis(); |
199 | final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x1, y1, 0); | 190 | final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x1, y1, 0); |
200 | downTime += 1000; | 191 | downTime += 1000; |
201 | final MotionEvent moveEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_MOVE, x2, y2, 0); | 192 | final MotionEvent moveEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_MOVE, x2, y2, 0); |
202 | final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_UP, x2, y2, 0); | 193 | final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_UP, x2, y2, 0); |
203 | binding.markView.onTouchEvent(downEvent); | 194 | binding.markView.onTouchEvent(downEvent); |
204 | binding.markView.onTouchEvent(moveEvent); | 195 | binding.markView.onTouchEvent(moveEvent); |
205 | binding.markView.onTouchEvent(upEvent); | 196 | binding.markView.onTouchEvent(upEvent); |
206 | downEvent.recycle(); | 197 | downEvent.recycle(); |
207 | moveEvent.recycle(); | 198 | moveEvent.recycle(); |
208 | upEvent.recycle(); | 199 | upEvent.recycle(); |
209 | } | 200 | } |
210 | 201 | ||
211 | @Override | 202 | @Override |
212 | protected ActivityImageEditBinding getViewBinding() { | 203 | protected ActivityImageEditBinding getViewBinding() { |
213 | return ActivityImageEditBinding.inflate(getLayoutInflater()); | 204 | return ActivityImageEditBinding.inflate(getLayoutInflater()); |
214 | } | 205 | } |
215 | 206 | ||
216 | @Override | 207 | @Override |
217 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | 208 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
218 | super.onActivityResult(requestCode, resultCode, data); | 209 | super.onActivityResult(requestCode, resultCode, data); |
219 | if (resultCode == RESULT_OK && !TextUtils.isEmpty(filepath)) { | 210 | if (resultCode == RESULT_OK && !TextUtils.isEmpty(filepath)) { |
220 | bitmap = BitmapFactory.decodeFile(filepath); | 211 | bitmap = BitmapFactory.decodeFile(filepath); |
221 | showImage(false); | 212 | showImage(false); |
222 | } | 213 | } |
223 | } | 214 | } |
224 | } | 215 | } |
225 | 216 |