Commit e88528e7ad8ba711bdf228b4f44d2c2e9d02ce2a

Authored by shixianjie
1 parent 67fd5373bf
Exists in master

bugfix: 去掉二次压缩

app/src/main/java/com/hjx/parent/JudgeActivity.java
1 package com.hjx.parent; 1 package com.hjx.parent;
2 2
3 import android.annotation.SuppressLint; 3 import android.annotation.SuppressLint;
4 import android.content.Intent; 4 import android.content.Intent;
5 import android.graphics.Bitmap; 5 import android.graphics.Bitmap;
6 import android.graphics.Rect; 6 import android.graphics.Rect;
7 import android.os.Bundle; 7 import android.os.Bundle;
8 import android.util.Log; 8 import android.util.Log;
9 9
10 import com.bumptech.glide.Glide; 10 import com.bumptech.glide.Glide;
11 import com.hjx.parent.api.JudgeRepository; 11 import com.hjx.parent.api.JudgeRepository;
12 import com.hjx.parent.databinding.ActivityJudgeBinding; 12 import com.hjx.parent.databinding.ActivityJudgeBinding;
13 import com.hjx.parent.databinding.LayoutJudgeRectBinding; 13 import com.hjx.parent.databinding.LayoutJudgeRectBinding;
14 import com.hjx.parent.dialog.ErrorInputDialog; 14 import com.hjx.parent.dialog.ErrorInputDialog;
15 import com.hjx.parent.rx.BaseRxActivity; 15 import com.hjx.parent.rx.BaseRxActivity;
16 import com.hjx.parent.utils.CutUtil; 16 import com.hjx.parent.utils.CutUtil;
17 import com.prws.common.bean.TopicBean; 17 import com.prws.common.bean.TopicBean;
18 18
19 import java.io.File; 19 import java.io.File;
20 import java.util.ArrayList; 20 import java.util.ArrayList;
21 import java.util.List; 21 import java.util.List;
22 import java.util.stream.Collectors; 22 import java.util.stream.Collectors;
23 23
24 import io.reactivex.Observable; 24 import io.reactivex.Observable;
25 import io.reactivex.Single; 25 import io.reactivex.Single;
26 import top.zibin.luban.Luban; 26 import top.zibin.luban.Luban;
27 27
28 public class JudgeActivity extends BaseRxActivity<ActivityJudgeBinding> { 28 public class JudgeActivity extends BaseRxActivity<ActivityJudgeBinding> {
29 29
30 private Bitmap bitmap; 30 private Bitmap bitmap;
31 31
32 private final List<JudgeCut> mList = new ArrayList<>(); 32 private final List<JudgeCut> mList = new ArrayList<>();
33 33
34 @SuppressLint("CheckResult") 34 @SuppressLint("CheckResult")
35 @Override 35 @Override
36 public void initView(Bundle savedInstanceState) { 36 public void initView(Bundle savedInstanceState) {
37 binding.ivBack.setOnClickListener(v -> { 37 binding.ivBack.setOnClickListener(v -> {
38 onBackPressed(); 38 onBackPressed();
39 }); 39 });
40 String path = getIntent().getStringExtra("path"); 40 String path = getIntent().getStringExtra("path");
41 if (path == null) return; 41 if (path == null) return;
42 final ArrayList<Rect> rects = getIntent().getParcelableArrayListExtra("rects"); 42 final ArrayList<Rect> rects = getIntent().getParcelableArrayListExtra("rects");
43 if (rects == null) return; 43 if (rects == null) return;
44 44
45 Single.just(path) 45 Single.just(path)
46 .map(origin -> Luban.with(this).load(origin).ignoreBy(300).get().get(0)) 46 .map(File::new)
47 .map(file -> Glide.with(this).asBitmap().load(file).submit().get()) 47 .map(file -> Glide.with(this).asBitmap().load(file).submit().get())
48 .compose(transformSingle()) 48 .compose(transformSingle())
49 .subscribe((bitmap, th) -> { 49 .subscribe((bitmap, th) -> {
50 if (th != null) th.printStackTrace(); 50 if (th != null) th.printStackTrace();
51 if (bitmap == null) return; 51 if (bitmap == null) return;
52 this.bitmap = bitmap; 52 this.bitmap = bitmap;
53 53
54 binding.ivPic.setImageBitmap(bitmap); 54 binding.ivPic.setImageBitmap(bitmap);
55 CutUtil.onLayoutReady(binding.ivPic, v -> { 55 CutUtil.onLayoutReady(binding.ivPic, v -> {
56 prepareRects(rects); 56 prepareRects(rects);
57 }); 57 });
58 }); 58 });
59 binding.btnSelectAll.setOnClickListener(v -> { 59 binding.btnSelectAll.setOnClickListener(v -> {
60 if (mList.isEmpty()) return; 60 if (mList.isEmpty()) return;
61 mList.forEach(it -> it.vb.getRoot().setSelected(true)); 61 mList.forEach(it -> it.vb.getRoot().setSelected(true));
62 checkCount(); 62 checkCount();
63 }); 63 });
64 binding.btnSelectNone.setOnClickListener(v -> { 64 binding.btnSelectNone.setOnClickListener(v -> {
65 if (mList.isEmpty()) return; 65 if (mList.isEmpty()) return;
66 mList.forEach(it -> it.vb.getRoot().setSelected(false)); 66 mList.forEach(it -> it.vb.getRoot().setSelected(false));
67 checkCount(); 67 checkCount();
68 }); 68 });
69 binding.btnAdd.setOnClickListener(v -> { 69 binding.btnAdd.setOnClickListener(v -> {
70 if (mList.isEmpty()) return; 70 if (mList.isEmpty()) return;
71 List<TopicBean> selected = mList.stream() 71 List<TopicBean> selected = mList.stream()
72 .filter(it -> it.vb.getRoot().isSelected()) 72 .filter(it -> it.vb.getRoot().isSelected())
73 .map(it -> { 73 .map(it -> {
74 TopicBean bean = new TopicBean(); 74 TopicBean bean = new TopicBean();
75 bean.setPath(it.getPath()); 75 bean.setPath(it.getPath());
76 return bean; 76 return bean;
77 }) 77 })
78 .collect(Collectors.toList()); 78 .collect(Collectors.toList());
79 79
80 ErrorInputDialog dialog = new ErrorInputDialog(this, selected); 80 ErrorInputDialog dialog = new ErrorInputDialog(this, selected);
81 dialog.show(() -> { 81 dialog.show(() -> {
82 showToast("已加入错题本"); 82 showToast("已加入错题本");
83 finish(); 83 finish();
84 }); 84 });
85 }); 85 });
86 } 86 }
87 87
88 private void prepareRects(List<Rect> rects) { 88 private void prepareRects(List<Rect> rects) {
89 mList.clear(); 89 mList.clear();
90 binding.flRects.removeAllViews(); 90 binding.flRects.removeAllViews();
91 float[] measuredSize = CutUtil.measureBitmap(binding.ivPic, bitmap); 91 float[] measuredSize = CutUtil.measureBitmap(binding.ivPic, bitmap);
92 for (int i = 0; i < rects.size(); i++) { 92 for (int i = 0; i < rects.size(); i++) {
93 Rect it = rects.get(i); 93 Rect it = rects.get(i);
94 Bitmap bitmap = CutUtil.cut(this.bitmap, it.left, it.top, it.width(), it.height(), this); 94 Bitmap bitmap = CutUtil.cut(this.bitmap, it.left, it.top, it.width(), it.height(), this);
95 Rect rect = measureRect(it, measuredSize); 95 Rect rect = measureRect(it, measuredSize);
96 LayoutJudgeRectBinding vb = showRect(rect); 96 LayoutJudgeRectBinding vb = showRect(rect);
97 mList.add(new JudgeCut(i, bitmap, rect, vb)); 97 mList.add(new JudgeCut(i, bitmap, rect, vb));
98 } 98 }
99 99
100 judge(); 100 judge();
101 } 101 }
102 102
103 @SuppressLint("CheckResult") 103 @SuppressLint("CheckResult")
104 private void judge() { 104 private void judge() {
105 showLoadingDialog(""); 105 showLoadingDialog("");
106 Observable.fromIterable(mList) 106 Observable.fromIterable(mList)
107 .map(it -> { 107 .map(it -> {
108 if (it.url != null && !it.url.isEmpty()) return it; 108 if (it.url != null && !it.url.isEmpty()) return it;
109 String base64 = CutUtil.bitmapToBase64(it.bitmap, true); 109 String base64 = CutUtil.bitmapToBase64(it.bitmap, true);
110 it.url = base64; 110 it.url = base64;
111 return it; 111 return it;
112 }) 112 })
113 .flatMap(it -> JudgeRepository.singleJudge(it.url) 113 .flatMap(it -> JudgeRepository.singleJudge(it.url)
114 .map(result -> { 114 .map(result -> {
115 it.correctResult = result; 115 it.correctResult = result;
116 return it; 116 return it;
117 }) 117 })
118 .toObservable() 118 .toObservable()
119 ) 119 )
120 .compose(transform()) 120 .compose(transform())
121 .subscribe(it -> { 121 .subscribe(it -> {
122 if (it.correctResult == 1) { 122 if (it.correctResult == 1) {
123 it.vb.ivResult.setImageResource(R.drawable.png_ic_judge_correct); 123 it.vb.ivResult.setImageResource(R.drawable.png_ic_judge_correct);
124 } else if (it.correctResult == 2) { 124 } else if (it.correctResult == 2) {
125 it.vb.ivResult.setImageResource(R.drawable.png_ic_judge_wrong); 125 it.vb.ivResult.setImageResource(R.drawable.png_ic_judge_wrong);
126 } 126 }
127 it.vb.getRoot().setSelected(it.correctResult != 1); 127 it.vb.getRoot().setSelected(it.correctResult != 1);
128 checkCount(); 128 checkCount();
129 }, th -> { 129 }, th -> {
130 cancelLoadingDialog(); 130 cancelLoadingDialog();
131 Log.e(getClass().getName(), "", th); 131 Log.e(getClass().getName(), "", th);
132 }, this::cancelLoadingDialog) 132 }, this::cancelLoadingDialog)
133 ; 133 ;
134 } 134 }
135 135
136 private LayoutJudgeRectBinding showRect(Rect rect) { 136 private LayoutJudgeRectBinding showRect(Rect rect) {
137 LayoutJudgeRectBinding vb = LayoutJudgeRectBinding.inflate(getLayoutInflater(), binding.flRects, false); 137 LayoutJudgeRectBinding vb = LayoutJudgeRectBinding.inflate(getLayoutInflater(), binding.flRects, false);
138 138
139 vb.getRoot().setMinimumWidth(rect.width()); 139 vb.getRoot().setMinimumWidth(rect.width());
140 vb.getRoot().setMinimumHeight(rect.height()); 140 vb.getRoot().setMinimumHeight(rect.height());
141 vb.getRoot().setTranslationX((float) rect.left); 141 vb.getRoot().setTranslationX((float) rect.left);
142 vb.getRoot().setTranslationY((float) rect.top); 142 vb.getRoot().setTranslationY((float) rect.top);
143 vb.getRoot().setOnClickListener(v -> { 143 vb.getRoot().setOnClickListener(v -> {
144 v.setSelected(!v.isSelected()); 144 v.setSelected(!v.isSelected());
145 checkCount(); 145 checkCount();
146 }); 146 });
147 binding.flRects.addView(vb.getRoot()); 147 binding.flRects.addView(vb.getRoot());
148 148
149 return vb; 149 return vb;
150 } 150 }
151 151
152 @SuppressLint("SetTextI18n") 152 @SuppressLint("SetTextI18n")
153 private void checkCount() { 153 private void checkCount() {
154 int count = (int) mList.stream().filter(it -> it.vb.getRoot().isSelected()).count(); 154 int count = (int) mList.stream().filter(it -> it.vb.getRoot().isSelected()).count();
155 if (count == 0) binding.btnAdd.setText("加入错题"); 155 if (count == 0) binding.btnAdd.setText("加入错题");
156 else binding.btnAdd.setText("加入错题(" + count + ")"); 156 else binding.btnAdd.setText("加入错题(" + count + ")");
157 binding.btnAdd.setEnabled(count > 0); 157 binding.btnAdd.setEnabled(count > 0);
158 } 158 }
159 159
160 /** bitmapRect 转换为 viewRect */ 160 /** bitmapRect 转换为 viewRect */
161 private Rect measureRect(Rect rect, float[] measuredSize) { 161 private Rect measureRect(Rect rect, float[] measuredSize) {
162 float realW = measuredSize[0]; 162 float realW = measuredSize[0];
163 float realH = measuredSize[1]; 163 float realH = measuredSize[1];
164 float offsetX = measuredSize[2]; 164 float offsetX = measuredSize[2];
165 float offsetY = measuredSize[3]; 165 float offsetY = measuredSize[3];
166 float percentX = realW / bitmap.getWidth(); 166 float percentX = realW / bitmap.getWidth();
167 float percentY = realH / bitmap.getHeight(); 167 float percentY = realH / bitmap.getHeight();
168 168
169 float left = rect.left * percentX + offsetX; 169 float left = rect.left * percentX + offsetX;
170 float right = rect.right * percentX + offsetX; 170 float right = rect.right * percentX + offsetX;
171 float top = rect.top * percentY + offsetY; 171 float top = rect.top * percentY + offsetY;
172 float bottom = rect.bottom * percentY + offsetY; 172 float bottom = rect.bottom * percentY + offsetY;
173 return new Rect((int) left, (int) top, (int) right, (int) bottom); 173 return new Rect((int) left, (int) top, (int) right, (int) bottom);
174 } 174 }
175 175
176 @Override 176 @Override
177 protected ActivityJudgeBinding getViewBinding() { 177 protected ActivityJudgeBinding getViewBinding() {
178 return ActivityJudgeBinding.inflate(getLayoutInflater()); 178 return ActivityJudgeBinding.inflate(getLayoutInflater());
179 } 179 }
180 180
181 static class JudgeCut { 181 static class JudgeCut {
182 final int index; 182 final int index;
183 final Bitmap bitmap; 183 final Bitmap bitmap;
184 final Rect rect; 184 final Rect rect;
185 final LayoutJudgeRectBinding vb; 185 final LayoutJudgeRectBinding vb;
186 186
187 String url; 187 String url;
188 188
189 /** 0:未批改;1:正确;2:错误;3:未作答 */ 189 /** 0:未批改;1:正确;2:错误;3:未作答 */
190 int correctResult = 0; 190 int correctResult = 0;
191 191
192 public JudgeCut(int index, Bitmap bitmap, Rect rect, LayoutJudgeRectBinding vb) { 192 public JudgeCut(int index, Bitmap bitmap, Rect rect, LayoutJudgeRectBinding vb) {
193 this.index = index; 193 this.index = index;
194 this.bitmap = bitmap; 194 this.bitmap = bitmap;
195 this.rect = rect; 195 this.rect = rect;
196 this.vb = vb; 196 this.vb = vb;
197 } 197 }
198 198
199 private String path; 199 private String path;
200 200
201 public String getPath() { 201 public String getPath() {
202 if (path != null) return path; 202 if (path != null) return path;
203 path = CutUtil.saveBitmapToCache(bitmap, App.getInstance()); 203 path = CutUtil.saveBitmapToCache(bitmap, App.getInstance());
204 return path; 204 return path;
205 } 205 }
206 } 206 }
207 } 207 }
208 208