refactor: Code cleanup
This commit is contained in:
parent
5a9842cdeb
commit
8e2eda15ce
|
|
@ -34,60 +34,46 @@ final class ImageConverterAndroid implements ImageConverterPlatform {
|
||||||
OutputFormat format = OutputFormat.jpeg,
|
OutputFormat format = OutputFormat.jpeg,
|
||||||
int quality = 100,
|
int quality = 100,
|
||||||
}) async {
|
}) async {
|
||||||
final inputJBytes = JByteArray.from(inputData);
|
JByteArray? inputJBytes;
|
||||||
|
Bitmap? bitmap;
|
||||||
|
Bitmap$CompressFormat? compressFormat;
|
||||||
|
ByteArrayOutputStream? outputStream;
|
||||||
|
JByteArray? outputJBytes;
|
||||||
try {
|
try {
|
||||||
final bitmap = BitmapFactory.decodeByteArray(
|
inputJBytes = JByteArray.from(inputData);
|
||||||
inputJBytes,
|
bitmap = BitmapFactory.decodeByteArray(inputJBytes, 0, inputData.length);
|
||||||
0,
|
|
||||||
inputData.length,
|
|
||||||
);
|
|
||||||
if (bitmap == null) {
|
if (bitmap == null) {
|
||||||
throw Exception('Failed to decode image. Invalid image data.');
|
throw Exception('Failed to decode image. Invalid image data.');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
compressFormat = switch (format) {
|
||||||
final compressFormat = switch (format) {
|
OutputFormat.jpeg => Bitmap$CompressFormat.JPEG,
|
||||||
OutputFormat.jpeg => Bitmap$CompressFormat.JPEG,
|
OutputFormat.png => Bitmap$CompressFormat.PNG,
|
||||||
OutputFormat.png => Bitmap$CompressFormat.PNG,
|
OutputFormat.webp => Bitmap$CompressFormat.WEBP_LOSSY,
|
||||||
OutputFormat.webp => Bitmap$CompressFormat.WEBP_LOSSY,
|
OutputFormat.heic => throw UnsupportedError(
|
||||||
OutputFormat.heic => throw UnsupportedError(
|
'HEIC output format is not supported on Android.',
|
||||||
'HEIC output format is not supported on Android.',
|
),
|
||||||
),
|
};
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
outputStream = ByteArrayOutputStream();
|
||||||
final outputStream = ByteArrayOutputStream();
|
final success = bitmap.compress(compressFormat, quality, outputStream);
|
||||||
try {
|
if (!success) {
|
||||||
final success = bitmap.compress(
|
throw Exception('Failed to compress bitmap.');
|
||||||
compressFormat,
|
|
||||||
quality,
|
|
||||||
outputStream,
|
|
||||||
);
|
|
||||||
if (!success) {
|
|
||||||
throw Exception('Failed to compress bitmap.');
|
|
||||||
}
|
|
||||||
|
|
||||||
final outputJBytes = outputStream.toByteArray();
|
|
||||||
if (outputJBytes == null) {
|
|
||||||
throw Exception('Failed to get byte array from output stream.');
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
final outputList = outputJBytes.toList();
|
|
||||||
return Uint8List.fromList(outputList);
|
|
||||||
} finally {
|
|
||||||
outputJBytes.release();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
outputStream.release();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
compressFormat.release();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
bitmap.release();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
outputJBytes = outputStream.toByteArray();
|
||||||
|
if (outputJBytes == null) {
|
||||||
|
throw Exception('Failed to get byte array from output stream.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return Uint8List.fromList(outputJBytes.toList());
|
||||||
} finally {
|
} finally {
|
||||||
inputJBytes.release();
|
inputJBytes?.release();
|
||||||
|
bitmap?.recycle();
|
||||||
|
bitmap?.release();
|
||||||
|
compressFormat?.release();
|
||||||
|
outputStream?.release();
|
||||||
|
outputJBytes?.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue