-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCropImageIntentBuilder.java
246 lines (216 loc) · 7.37 KB
/
CropImageIntentBuilder.java
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
/*
* Copyright (C) 2012 Lorenzo Villani.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.camera;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
/**
* By default the following features are enabled, unless you override them by calling setters in the
* builder:
*
* <ul>
* <li>Scale;</li>
* <li>Scale up (if needed);</li>
* <li>Face detection;</li>
* </ul>
*
* @since 1.0.1
*/
// TODO: Circle Crop
// TODO: Set Wallpaper
public class CropImageIntentBuilder {
private static final String EXTRA_ASPECT_X = "aspectX";
private static final String EXTRA_ASPECT_Y = "aspectY";
private static final String EXTRA_OUTPUT_X = "outputX";
private static final String EXTRA_OUTPUT_Y = "outputY";
private static final String EXTRA_BITMAP_DATA = "data";
private static final String EXTRA_SCALE = "scale";
private static final String EXTRA_SCALE_UP_IF_NEEDED = "scaleUpIfNeeded";
private static final String EXTRA_NO_FACE_DETECTION = "noFaceDetection";
private static final String EXTRA_CIRCLE_CROP = "circleCrop";
private static final String EXTRA_OUTPUT_FORMAT = "outputFormat";
private static final int DEFAULT_SCALE = 1;
private boolean scale = true;
private boolean scaleUpIfNeeded = true;
private boolean doFaceDetection = true;
private boolean circleCrop = false;
private String outputFormat = null;
private Uri sourceImage;
private Bitmap bitmap;
private final int aspectX;
private final int aspectY;
private final int outputX;
private final int outputY;
private final Uri saveUri;
/**
* Constructor.
*
* @param outputX
* Output vertical size in pixels.
* @param outputY
* Output horizontal size in pixels.
* @param saveUri
* Output file URI.
* @since 1.0.1
*/
public CropImageIntentBuilder(final int outputX, final int outputY, final Uri saveUri) {
this(DEFAULT_SCALE, DEFAULT_SCALE, outputX, outputY, saveUri);
}
/**
* Constructor.
*
* @param aspectX
* Horizontal aspect ratio.
* @param aspectY
* Vertical aspect ratio.
* @param outputX
* Output vertical size in pixels.
* @param outputY
* Output horizontal size in pixels.
* @param saveUri
* Output file URI.
* @since 1.0.1
*/
public CropImageIntentBuilder(final int aspectX, final int aspectY, final int outputX,
final int outputY, final Uri saveUri) {
this.aspectX = aspectX;
this.aspectY = aspectY;
this.outputX = outputX;
this.outputY = outputY;
this.saveUri = saveUri;
}
/**
* Builds the Intent.
*
* @param context
* The application context.
* @return The newly created intent.
* @since 1.0.1
*/
public Intent getIntent(final Context context) {
final Intent intent = new Intent(context, CropImage.class);
//
// Required Intent extras.
//
intent.putExtra(EXTRA_ASPECT_X, this.aspectX);
intent.putExtra(EXTRA_ASPECT_Y, this.aspectY);
intent.putExtra(EXTRA_OUTPUT_X, this.outputX);
intent.putExtra(EXTRA_OUTPUT_Y, this.outputY);
intent.putExtra(MediaStore.EXTRA_OUTPUT, this.saveUri);
//
// Optional Intent Extras
//
intent.putExtra(EXTRA_SCALE, this.scale);
intent.putExtra(EXTRA_SCALE_UP_IF_NEEDED, this.scaleUpIfNeeded);
intent.putExtra(EXTRA_NO_FACE_DETECTION, !this.doFaceDetection);
intent.putExtra(EXTRA_CIRCLE_CROP, this.circleCrop);
intent.putExtra(EXTRA_OUTPUT_FORMAT, this.outputFormat);
if (this.bitmap != null) {
intent.putExtra(EXTRA_BITMAP_DATA, this.bitmap);
}
if (this.sourceImage != null) {
intent.setData(this.sourceImage);
}
return intent;
}
/**
* Scales down the picture.
*
* @param scale
* Whether to scale down the image.
* @return This Builder object to allow for chaining of calls to set methods.
* @since 1.0.1
*/
public CropImageIntentBuilder setScale(final boolean scale) {
this.scale = scale;
return this;
}
/**
* Whether to scale up the image if the cropped region is smaller than the output size.
*
* @param scaleUpIfNeeded
* Whether to scale up the image.
* @return This Builder object to allow for chaining of calls to set methods.
* @since 1.0.1
*/
public CropImageIntentBuilder setScaleUpIfNeeded(final boolean scaleUpIfNeeded) {
this.scaleUpIfNeeded = scaleUpIfNeeded;
return this;
}
/**
* Performs face detection before allowing users to crop the image.
*
* @param doFaceDetection
* Whether to perform face detection.
* @return This Builder object to allow for chaining of calls to set methods.
* @since 1.0.1
*/
public CropImageIntentBuilder setDoFaceDetection(final boolean doFaceDetection) {
this.doFaceDetection = doFaceDetection;
return this;
}
/**
* Sets bitmap data to crop. Please note that this method overrides any source image set by
* {@link #setSourceImage(Uri)}.
*
* @param bitmap
* The {@link Bitmap} to crop.
* @return This Builder object to allow for chaining of calls to set methods.
* @since 1.0.1
*/
public CropImageIntentBuilder setBitmap(final Bitmap bitmap) {
this.bitmap = bitmap;
return this;
}
/**
* Sets the Uri of the image to crop. It must be accessible to the calling application/activity.
*
* @param sourceImage
* Uri of the image to crop.
* @return This Builder object to allow for chaining of calls to set methods.
* @since 1.0.1
*/
public CropImageIntentBuilder setSourceImage(final Uri sourceImage) {
this.sourceImage = sourceImage;
return this;
}
/**
* Whether to crop the image as circle
*
* @param circleCrop
* Whether to crop the image as circle.
* @return This Builder object to allow for chaining of calls to set methods.
* @since 1.0.1
*/
public CropImageIntentBuilder setCircleCrop(final boolean circleCrop) {
this.circleCrop = circleCrop;
return this;
}
/**
* Set output format of the image to crop. If not set, JPEG will be used.
*
* @param outputFormat
* Output format of the image to crop, such as JPEG, PNG or WEBP.
* @return This Builder object to allow for chaining of calls to set methods.
* @since 1.0.1
*/
public CropImageIntentBuilder setOutputFormat(final String outputFormat) {
this.outputFormat = outputFormat;
return this;
}
}