Refactor
This commit is contained in:
1 parent
89935b5639
commit
9d25376e1f
4 files changed
+45
-29
No files matched your search
@@ -1097,33 +1097,6 @@ public final class SpiderQueen implements Runnable {
|
||||
void onGetImageFailure(int index, String error);
|
||||
}
|
||||
|
||||
private static class AutoCloseInputStream extends InputStream {
|
||||
|
||||
private final InputStreamPipe mPipe;
|
||||
private final InputStream mIs;
|
||||
|
||||
public AutoCloseInputStream(InputStreamPipe pipe, InputStream is) {
|
||||
mPipe = pipe;
|
||||
mIs = is;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return mIs.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(@NonNull byte[] buffer, int byteOffset, int byteCount) throws IOException {
|
||||
return mIs.read(buffer, byteOffset, byteCount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
mPipe.close();
|
||||
mPipe.release();
|
||||
}
|
||||
}
|
||||
|
||||
private class SpiderWorker implements Runnable {
|
||||
|
||||
private final long mGid;
|
||||
@@ -1694,7 +1667,7 @@ public final class SpiderQueen implements Runnable {
|
||||
|
||||
pipe.obtain();
|
||||
try {
|
||||
is = new AutoCloseInputStream(pipe, pipe.open());
|
||||
is = pipe.open();
|
||||
} catch (IOException e) {
|
||||
// Can't open pipe
|
||||
error = GetText.getString(R.string.error_reading_failed);
|
||||
@@ -1709,6 +1682,12 @@ public final class SpiderQueen implements Runnable {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
image = null;
|
||||
} finally {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (image == null) {
|
||||
error = GetText.getString(R.string.error_decoding_failed);
|
||||
|
||||
@@ -57,6 +57,14 @@ public final class Image {
|
||||
return nativeDecode(fd, partially);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode image from {@code InputStream}
|
||||
*/
|
||||
@Nullable
|
||||
public static Image decode(Integer fd, boolean partially) {
|
||||
return nativeDecodeFdInt(fd, partially);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a plain image from Bitmap
|
||||
*/
|
||||
@@ -75,6 +83,8 @@ public final class Image {
|
||||
|
||||
private static native Image nativeDecode(FileDescriptor fd, boolean partially);
|
||||
|
||||
private static native Image nativeDecodeFdInt(int fd, boolean partially);
|
||||
|
||||
private static native Image nativeCreate(Bitmap bitmap);
|
||||
|
||||
private static native int nativeGetByteCount(long nativePtr, int format);
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
|
||||
#include <android/imagedecoder.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "image.h"
|
||||
#include "image_plain.h"
|
||||
@@ -30,7 +31,10 @@
|
||||
void* decode(JNIEnv* env, int fd, bool partially, int* format)
|
||||
{
|
||||
AImageDecoder *decoder;
|
||||
AImageDecoder_createFromFd(fd, &decoder);
|
||||
int r = AImageDecoder_createFromFd(fd, &decoder);
|
||||
LOGD("%s%d", "Create ImageDecoder with ret ", r);
|
||||
if (r)
|
||||
return NULL;
|
||||
*format = AImageDecoder_isAnimated(decoder);
|
||||
AImageDecoderHeaderInfo *headerInfo = AImageDecoder_getHeaderInfo(decoder);
|
||||
IMAGE *image = malloc(sizeof(IMAGE));
|
||||
|
||||
@@ -108,6 +108,29 @@ Java_com_hippo_image_Image_nativeDecode(JNIEnv* env,
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_com_hippo_image_Image_nativeDecodeFdInt(JNIEnv* env,
|
||||
jclass clazz, jint fd, jboolean partially)
|
||||
{
|
||||
int format;
|
||||
void* image;
|
||||
jobject image_object;
|
||||
|
||||
image = decode(env, fd, partially, &format);
|
||||
if (image == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
image_object = create_image_object(env, image, format,
|
||||
get_width(image, format), get_height(image, format));
|
||||
if (image_object == NULL) {
|
||||
recycle(env, image, format);
|
||||
return NULL;
|
||||
} else {
|
||||
return image_object;
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_com_hippo_image_Image_nativeCreate(JNIEnv* env,
|
||||
jclass clazz, jobject bitmap)
|
||||
|
||||
Reference in new issue
Block a user