wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*  Copyright (C) 2020-2024 Daniel Dakhno
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.qhybrid;
 
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
 
import java.io.IOException;
 
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.image.AssetImage;
import nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid.requests.fossil_hr.image.AssetImageFactory;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
 
public class ImageEditActivity extends AbstractGBActivity implements View.OnTouchListener {
    static final public int RESULT_CODE_EDIT_SUCCESS = 0;
 
    ImageView overlay;
    Canvas overlayCanvas;
    Paint overlayPaint;
    Bitmap overlayBitmap, mainBitmap;
 
    float x = 0, y = 0, diameter = 0, imageDimension = 0;
    int imageWidth, imageHeight;
 
    private enum MovementState {
        MOVE_UPPER_LEFT,
        MOVE_LOWER_RIGHT,
        MOVE_FRAME
    }
    private MovementState movementState;
    float movementStartX, movementStartY, movementStartFrameX, movementStartFrameY, movementStartDiameter, leftUpperDeltaX, leftUpperDeltaY;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qhybrid_image_edit);
 
        final RelativeLayout mainLayout = findViewById(R.id.qhybrid_image_edit_container);
        overlay = findViewById(R.id.qhybrid_image_edit_image_overlay);
        overlay.setOnTouchListener(this);
 
        findViewById(R.id.qhybrid_image_edit_okay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finalizeImage();
            }
        });
 
        mainLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    fitImageToLayout(mainLayout);
                } catch (IOException | RuntimeException e) {
                    GB.log("Error formatting image", GB.ERROR, e);
                    GB.toast("Error formatting image", Toast.LENGTH_LONG, GB.ERROR);
                    finish();
                }
            }
        });
    }
 
    private void finalizeImage(){
        Bitmap cropped = Bitmap.createBitmap(this.mainBitmap, (int) this.x, (int) this.y, (int) this.diameter, (int) this.diameter);
        Bitmap scaled = Bitmap.createScaledBitmap(cropped, 400, 400, false);
        cropped.recycle();
 
        try {
            AssetImage image = AssetImageFactory.createAssetImage(scaled, false, 0, 0, 0);
 
            Intent resultIntent = new Intent();
            resultIntent.putExtra("EXTRA_PIXELS_ENCODED", image.getFileData());
            setResult(RESULT_CODE_EDIT_SUCCESS, resultIntent);
            finish();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
 
            scaled.recycle();
        }
    }
 
    private void fitImageToLayout(RelativeLayout mainLayout) throws IOException, RuntimeException {
        float containerHeight = mainLayout.getHeight();
        float containerWidth = mainLayout.getWidth();
        float containerRelation = containerHeight / containerWidth;
 
        Bitmap bitmap = this.createImageFromURI();
        float imageHeight = bitmap.getHeight();
        float imageWidth = bitmap.getWidth();
        float imageRelation = imageHeight / imageWidth;
 
        float scaleRatio;
        if(imageRelation > containerRelation){
            scaleRatio = containerHeight / imageHeight;
        }else{
            scaleRatio = containerWidth / imageWidth;
        }
 
        int scaledHeight = (int)(imageHeight * scaleRatio);
        int scaledWidth = (int)(imageWidth * scaleRatio);
 
        this.imageHeight = scaledHeight;
        this.imageWidth = scaledWidth;
 
        this.imageDimension = this.diameter = Math.min(scaledHeight, scaledWidth);
 
        mainBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, false);
 
        ImageView mainImageView = findViewById(R.id.qhybrid_image_edit_image);
        mainImageView.setImageBitmap(mainBitmap);
        createOverlay(scaledWidth, scaledHeight);
    }
 
    private void createOverlay(int width, int height){
        this.overlayBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
        this.overlayCanvas = new Canvas(this.overlayBitmap);
 
        this.overlayPaint = new Paint();
        this.overlayPaint.setColor(Color.BLACK);
        this.overlayPaint.setStyle(Paint.Style.STROKE);
        this.overlayPaint.setStrokeWidth(imageDimension / 100);
 
        renderOverlay();
    }
 
    private Bitmap createImageFromURI() throws IOException, RuntimeException {
        Uri imageURI = getIntent().getData();
        if (imageURI == null) {
            throw new RuntimeException("no image attached");
        }
 
        ContentResolver resolver = getContentResolver();
        Cursor c = resolver.query(imageURI, new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null);
        c.moveToFirst();
        int orientation = c.getInt(c.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
        c.close();
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageURI);
        if (orientation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(90);
 
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        }
 
        return bitmap;
    }
 
    private void renderOverlay() {
        overlayCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
 
        overlayCanvas.drawCircle(x, y, imageDimension / 15, overlayPaint);
        overlayCanvas.drawCircle(x + diameter, y + diameter, imageDimension / 15, overlayPaint);
 
        overlayCanvas.drawCircle(x + diameter / 2, y + diameter / 2, diameter / 2, overlayPaint);
 
        overlay.setImageBitmap(overlayBitmap);
    }
 
    private void forceImageBoundaries(){
        this.x = Math.max(this.x, 0);
        this.y = Math.max(this.y, 0);
 
        this.x = Math.min(this.x, this.imageWidth - diameter);
        this.y = Math.min(this.y, this.imageHeight - diameter);
    }
 
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
            handleTouchDown(motionEvent);
        }else if(motionEvent.getAction() == MotionEvent.ACTION_MOVE){
            handleTouchMove(motionEvent);
        }
        return true;
    }
 
    private void handleTouchMove(MotionEvent motionEvent){
        if(movementState == MovementState.MOVE_UPPER_LEFT){
            float moveDeltaX = motionEvent.getX() - this.movementStartX;
            float moveDeltaY = motionEvent.getY() - this.movementStartY;
 
            float mid = (moveDeltaX + moveDeltaY) / 2;
 
            this.diameter = this.movementStartDiameter - mid;
            this.x = this.movementStartX + mid + this.leftUpperDeltaX;
            this.y = this.movementStartY + mid + this.leftUpperDeltaY;
        }else if(movementState == MovementState.MOVE_LOWER_RIGHT) {
            float moveDeltaX = motionEvent.getX() - this.movementStartX;
            float moveDeltaY = motionEvent.getY() - this.movementStartY;
 
            float mid = (moveDeltaX + moveDeltaY) / 2;
 
            this.diameter = this.movementStartDiameter + mid;
        }else if(movementState == MovementState.MOVE_FRAME){
            this.x = this.movementStartFrameX + (motionEvent.getX() - this.movementStartX);
            this.y = this.movementStartFrameY + (motionEvent.getY() - this.movementStartY);
        }
 
        this.forceImageBoundaries();
        renderOverlay();
    }
 
    private void handleTouchDown(MotionEvent motionEvent) {
        this.movementStartX = motionEvent.getX();
        this.movementStartY = motionEvent.getY();
        this.movementStartFrameX = this.x;
        this.movementStartFrameY = this.y;
        this.movementStartDiameter = this.diameter;
 
        final float threshold = imageDimension / 15;
 
        float upperLeftDeltaX = this.x - motionEvent.getX();
        float upperLeftDeltaY = this.y - motionEvent.getY();
 
        float upperLeftDistance = (float) Math.sqrt(upperLeftDeltaX * upperLeftDeltaX + upperLeftDeltaY * upperLeftDeltaY);
 
        if(upperLeftDistance < threshold){
            // Toast.makeText(this, "upper left", 0).show();
            this.leftUpperDeltaX = upperLeftDeltaX;
            this.leftUpperDeltaY = upperLeftDeltaY;
            this.movementState = MovementState.MOVE_UPPER_LEFT;
            return;
        }
 
        float lowerLeftX = this.x + diameter;
        float lowerLeftY = this.y + diameter;
 
        float lowerRightDeltaX = lowerLeftX - motionEvent.getX();
        float lowerRightDeltaY = lowerLeftY - motionEvent.getY();
 
        float lowerRightDistance = (float) Math.sqrt(lowerRightDeltaX * lowerRightDeltaX + lowerRightDeltaY * lowerRightDeltaY);
 
        if(lowerRightDistance < threshold){
            // Toast.makeText(this, "lower right", 0).show();
            this.movementState = MovementState.MOVE_LOWER_RIGHT;
            return;
        }
 
        // Toast.makeText(this, "anywhere else", 0).show();
        this.movementState = MovementState.MOVE_FRAME;
    }
}