Skip to content

Commit d9c6f52

Browse files
authored
Basic android.graphics Rect and Canvas implementation (#461)
Some extensions use more Canvas methods, but they don't really seem to get that far yet, all the others I was able to test seem to work now.
1 parent 0a748cd commit d9c6f52

File tree

4 files changed

+173
-11
lines changed

4 files changed

+173
-11
lines changed

AndroidCompat/build.gradle.kts

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ dependencies {
2626
// Android version of SimpleDateFormat
2727
implementation("com.ibm.icu:icu4j:72.1")
2828

29-
// OpenJDK lacks a native JPEG encoder
29+
// OpenJDK lacks native JPEG encoder and native WEBP decoder
3030
implementation("com.twelvemonkeys.common:common-lang:3.9.4")
31+
implementation("com.twelvemonkeys.common:common-io:3.9.4")
32+
implementation("com.twelvemonkeys.common:common-image:3.9.4")
3133
implementation("com.twelvemonkeys.imageio:imageio-core:3.9.4")
34+
implementation("com.twelvemonkeys.imageio:imageio-metadata:3.9.4")
3235
implementation("com.twelvemonkeys.imageio:imageio-jpeg:3.4.1")
36+
implementation("com.twelvemonkeys.imageio:imageio-webp:3.9.4")
3337
}

AndroidCompat/src/main/java/android/graphics/BitmapFactory.java

+25-10
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,48 @@
44
import java.io.ByteArrayInputStream;
55
import java.io.InputStream;
66
import java.io.IOException;
7+
import java.util.Iterator;
78
import javax.imageio.ImageIO;
9+
import javax.imageio.ImageReader;
10+
import javax.imageio.stream.ImageInputStream;
811

912
public class BitmapFactory {
10-
public static Bitmap decodeStream(InputStream is) {
11-
Bitmap bm = null;
13+
public static Bitmap decodeStream(InputStream inputStream) {
14+
Bitmap bitmap = null;
1215

1316
try {
14-
BufferedImage bf = ImageIO.read(is);
15-
bm = new Bitmap(bf);
17+
ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
18+
Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
19+
20+
if (!imageReaders.hasNext()) {
21+
throw new IllegalArgumentException("no reader for image");
22+
}
23+
24+
ImageReader imageReader = imageReaders.next();
25+
imageReader.setInput(imageInputStream);
26+
27+
BufferedImage image = imageReader.read(0, imageReader.getDefaultReadParam());
28+
bitmap = new Bitmap(image);
29+
30+
imageReader.dispose();
1631
} catch (IOException ex) {
1732
throw new RuntimeException(ex);
1833
}
1934

20-
return bm;
35+
return bitmap;
2136
}
2237

2338
public static Bitmap decodeByteArray(byte[] data, int offset, int length) {
24-
Bitmap bm = null;
39+
Bitmap bitmap = null;
2540

26-
ByteArrayInputStream bais = new ByteArrayInputStream(data);
41+
ByteArrayInputStream byteArrayStream = new ByteArrayInputStream(data);
2742
try {
28-
BufferedImage bf = ImageIO.read(bais);
29-
bm = new Bitmap(bf);
43+
BufferedImage image = ImageIO.read(byteArrayStream);
44+
bitmap = new Bitmap(image);
3045
} catch (IOException ex) {
3146
throw new RuntimeException(ex);
3247
}
3348

34-
return bm;
49+
return bitmap;
3550
}
3651
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package android.graphics;
2+
3+
import java.awt.Graphics2D;
4+
import java.awt.image.BufferedImage;
5+
import javax.imageio.ImageIO;
6+
7+
public final class Canvas {
8+
private BufferedImage canvasImage;
9+
private Graphics2D canvas;
10+
11+
public Canvas(Bitmap bitmap) {
12+
canvasImage = bitmap.getImage();
13+
canvas = canvasImage.createGraphics();
14+
}
15+
16+
public void drawBitmap(Bitmap sourceBitmap, Rect src, Rect dst, Paint paint) {
17+
BufferedImage sourceImage = sourceBitmap.getImage();
18+
BufferedImage sourceImageCropped = sourceImage.getSubimage(src.left, src.top, src.getWidth(), src.getHeight());
19+
canvas.drawImage(sourceImageCropped, null, dst.left, dst.top);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package android.graphics;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
public final class Rect {
9+
int left;
10+
int top;
11+
int right;
12+
int bottom;
13+
14+
private static final class UnflattenHelper {
15+
private static final Pattern FLATTENED_PATTERN = Pattern.compile(
16+
"(-?\\d+) (-?\\d+) (-?\\d+) (-?\\d+)");
17+
18+
static Matcher getMatcher(String str) {
19+
return FLATTENED_PATTERN.matcher(str);
20+
}
21+
}
22+
23+
public Rect() {
24+
}
25+
26+
public Rect(int left, int top, int right, int bottom) {
27+
this.left = left;
28+
this.top = top;
29+
this.right = right;
30+
this.bottom = bottom;
31+
}
32+
33+
public Rect(Rect r) {
34+
if (r == null) {
35+
this.left = 0;
36+
this.top = 0;
37+
this.right = 0;
38+
this.bottom = 0;
39+
} else {
40+
this.left = left;
41+
this.top = top;
42+
this.right = right;
43+
this.bottom = bottom;
44+
}
45+
}
46+
47+
public final int getWidth() {
48+
return right - left;
49+
}
50+
51+
public final int getHeight() {
52+
return bottom - top;
53+
}
54+
55+
public static Rect unflattenFromString(String str) {
56+
if (str.isEmpty()) {
57+
return null;
58+
}
59+
60+
Matcher matcher = UnflattenHelper.getMatcher(str);
61+
if (!matcher.matches()) {
62+
return null;
63+
}
64+
65+
return new Rect(Integer.parseInt(matcher.group(1)),
66+
Integer.parseInt(matcher.group(2)),
67+
Integer.parseInt(matcher.group(3)),
68+
Integer.parseInt(matcher.group(4)));
69+
}
70+
71+
public String toShortString() {
72+
return toShortString(new StringBuilder(32));
73+
}
74+
75+
public String toShortString(StringBuilder sb) {
76+
sb.setLength(0);
77+
sb.append('['); sb.append(left); sb.append(',');
78+
sb.append(top); sb.append("]["); sb.append(right);
79+
sb.append(','); sb.append(bottom); sb.append(']');
80+
return sb.toString();
81+
}
82+
83+
public String flattenToString() {
84+
StringBuilder sb = new StringBuilder(32);
85+
sb.append(left);
86+
sb.append(' ');
87+
sb.append(top);
88+
sb.append(' ');
89+
sb.append(right);
90+
sb.append(' ');
91+
sb.append(bottom);
92+
return sb.toString();
93+
}
94+
95+
public void writeToParcel(Parcel out, int flags) {
96+
out.writeInt(left);
97+
out.writeInt(top);
98+
out.writeInt(right);
99+
out.writeInt(bottom);
100+
}
101+
102+
public static final Parcelable.Creator<Rect> CREATOR = new Parcelable.Creator<Rect>() {
103+
@Override
104+
public Rect createFromParcel(Parcel in) {
105+
Rect r = new Rect();
106+
r.readFromParcel(in);
107+
return r;
108+
}
109+
110+
@Override
111+
public Rect[] newArray(int size) {
112+
return new Rect[size];
113+
}
114+
};
115+
116+
public void readFromParcel(Parcel in) {
117+
left = in.readInt();
118+
top = in.readInt();
119+
right = in.readInt();
120+
bottom = in.readInt();
121+
}
122+
}

0 commit comments

Comments
 (0)