JudgeActivity.java
4.07 KB
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
package com.hjx.parent;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import com.bumptech.glide.Glide;
import com.hjx.parent.databinding.ActivityJudgeBinding;
import com.hjx.parent.databinding.LayoutJudgeRectBinding;
import com.hjx.parent.rx.BaseRxActivity;
import com.hjx.parent.utils.CutUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.Single;
import top.zibin.luban.Luban;
public class JudgeActivity extends BaseRxActivity<ActivityJudgeBinding> {
private Bitmap bitmap;
private final List<JudgeCut> mList = new ArrayList<>();
@SuppressLint("CheckResult")
@Override
public void initView(Bundle savedInstanceState) {
binding.ivBack.setOnClickListener(v -> {
onBackPressed();
});
String path = getIntent().getStringExtra("path");
if (path == null) return;
final ArrayList<Rect> rects = getIntent().getParcelableArrayListExtra("rects");
if (rects == null) return;
Single.just(path)
.map(origin -> Luban.with(this).load(origin).ignoreBy(300).get().get(0))
.map(file -> Glide.with(this).asBitmap().load(file).submit().get())
.compose(transformSingle())
.subscribe((bitmap, th) -> {
if (th != null) th.printStackTrace();
if (bitmap == null) return;
this.bitmap = bitmap;
binding.ivPic.setImageBitmap(bitmap);
CutUtil.onLayoutReady(binding.ivPic, v -> {
prepareRects(rects);
});
});
}
private void prepareRects(List<Rect> rects) {
mList.clear();
binding.flRects.removeAllViews();
float[] measuredSize = CutUtil.measureBitmap(binding.ivPic, bitmap);
for (int i = 0; i < rects.size(); i++) {
Rect it = rects.get(i);
Bitmap bitmap = CutUtil.cut(this.bitmap, it.left, it.top, it.width(), it.height(), this);
Rect rect = measureRect(it, measuredSize);
LayoutJudgeRectBinding vb = showRect(rect);
mList.add(new JudgeCut(i, bitmap, rect, vb));
}
}
private LayoutJudgeRectBinding showRect(Rect rect) {
LayoutJudgeRectBinding vb = LayoutJudgeRectBinding.inflate(getLayoutInflater(), binding.flRects, false);
vb.getRoot().setMinimumWidth(rect.width());
vb.getRoot().setMinimumHeight(rect.height());
vb.getRoot().setTranslationX((float) rect.left);
vb.getRoot().setTranslationY((float) rect.top);
vb.getRoot().setOnClickListener(v -> {
v.setSelected(!v.isSelected());
checkCount();
});
binding.flRects.addView(vb.getRoot());
return vb;
}
private void checkCount() {
//
}
/** bitmapRect 转换为 viewRect */
private Rect measureRect(Rect rect, float[] measuredSize) {
float realW = measuredSize[0];
float realH = measuredSize[1];
float offsetX = measuredSize[2];
float offsetY = measuredSize[3];
float percentX = realW / bitmap.getWidth();
float percentY = realH / bitmap.getHeight();
float left = rect.left * percentX + offsetX;
float right = rect.right * percentX + offsetX;
float top = rect.top * percentY + offsetY;
float bottom = rect.bottom * percentY + offsetY;
return new Rect((int) left, (int) top, (int) right, (int) bottom);
}
@Override
protected ActivityJudgeBinding getViewBinding() {
return ActivityJudgeBinding.inflate(getLayoutInflater());
}
static class JudgeCut {
final int index;
final Bitmap bitmap;
final Rect rect;
final LayoutJudgeRectBinding vb;
int correctResult = 0;
public JudgeCut(int index, Bitmap bitmap, Rect rect, LayoutJudgeRectBinding vb) {
this.index = index;
this.bitmap = bitmap;
this.rect = rect;
this.vb = vb;
}
}
}