GLImage.java 7.28 KB
Newer Older
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
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
package com.projectseptember.RNGL;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.support.annotation.Nullable;

import com.facebook.common.util.UriUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/*
This class is maintained and inspired from
https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.java
also inspired from
https://github.com/CyberAgent/android-gpuimage/blob/master/library/src/jp/co/cyberagent/android/gpuimage/GPUImage.java
 */
public class GLImage {
    private final Context context;
    private String src;
    private GLTexture texture;

    private Uri uri;
    private boolean isLocalImage;
    private boolean isDirty;
    private AsyncTask<Void, Void, Bitmap> task;
    private Runnable onload;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
35
    private RunInGLThread glScheduler;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
36

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
37
    public GLImage (Context context, RunInGLThread glScheduler, Runnable onload) {
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
38 39
        this.context = context;
        this.onload = onload;
Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
40
        this.glScheduler = glScheduler;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
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
        this.texture = new GLTexture();
    }

    public void setSrc(String src) {
        if (this.src == src) return;
        this.src = src;
        reloadImage();
    }

    private void reloadImage () {
        uri = null;
        if (src != null) {
            try {
                uri = Uri.parse(src);
                // Verify scheme is set, so that relative uri (used by static resources) are not handled.
                if (uri.getScheme() == null) {
                    uri = null;
                }
            } catch (Exception e) {
                // ignore malformed uri, then attempt to extract resource ID.
            }
            if (uri == null) {
                uri = getResourceDrawableUri(context, src);
                isLocalImage = true;
            } else {
                isLocalImage = false;
            }
        }
        isDirty = true;
    }

Gaëtan Renaudeau's avatar
wip  
Gaëtan Renaudeau committed
72 73 74 75 76 77 78
    public void onLoad (final Bitmap bitmap) {
        glScheduler.runInGLThread(new Runnable() {
            public void run() {
                texture.setPixels(bitmap);
                onload.run();
            }
        });
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
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
    }

    public GLTexture getTexture() {
        if (isDirty) {
            if (task != null) task.cancel(true);
            task = new LoadImageUriTask(this, uri).execute();
            isDirty = false;
        }
        return texture;
    }

    private static @Nullable Uri getResourceDrawableUri(Context context, @Nullable String name) {
        if (name == null || name.isEmpty()) {
            return null;
        }
        name = name.toLowerCase().replace("-", "_");
        int resId = context.getResources().getIdentifier(
                name,
                "drawable",
                context.getPackageName());
        return new Uri.Builder()
                .scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
                .path(String.valueOf(resId))
                .build();
    }


    private class LoadImageUriTask extends LoadImageTask {

        private final Uri mUri;

        public LoadImageUriTask(GLImage gpuImage, Uri uri) {
            super(gpuImage);
            mUri = uri;
        }

        @Override
        protected Bitmap decode(BitmapFactory.Options options) {
            try {
                InputStream inputStream;
                if (mUri.getScheme().startsWith("http") || mUri.getScheme().startsWith("https")) {
                    inputStream = new URL(mUri.toString()).openStream();
                } else {
                    inputStream = context.getContentResolver().openInputStream(mUri);
                }
                return BitmapFactory.decodeStream(inputStream, null, options);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected int getImageOrientation() throws IOException {
            Cursor cursor = context.getContentResolver().query(mUri,
                    new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

            if (cursor == null || cursor.getCount() != 1) {
                return 0;
            }

            cursor.moveToFirst();
            int orientation = cursor.getInt(0);
            cursor.close();
            return orientation;
        }
    }

    /*
    private class LoadImageFileTask extends LoadImageTask {

        private final File mImageFile;

        public LoadImageFileTask(GLImage gpuImage, File file) {
            super(gpuImage);
            mImageFile = file;
        }

        @Override
        protected Bitmap decode(BitmapFactory.Options options) {
            return BitmapFactory.decodeFile(mImageFile.getAbsolutePath(), options);
        }

        @Override
        protected int getImageOrientation() throws IOException {
            ExifInterface exif = new ExifInterface(mImageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            switch (orientation) {
                case ExifInterface.ORIENTATION_NORMAL:
                    return 0;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    return 90;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    return 180;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    return 270;
                default:
                    return 0;
            }
        }
    }
    */

    private abstract class LoadImageTask extends AsyncTask<Void, Void, Bitmap> {

        private GLImage glImage;

        public LoadImageTask (GLImage glImage) {
            this.glImage = glImage;
        }

        @Override
        protected Bitmap doInBackground(Void... params) {
            return loadResizedImage();
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            glImage.onLoad(bitmap);
        }

        protected abstract Bitmap decode(BitmapFactory.Options options);

        private Bitmap loadResizedImage() {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            decode(options);
            options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            options.inTempStorage = new byte[32 * 1024];
            Bitmap bitmap = decode(options);
            if (bitmap == null) {
                return null;
            }

Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
215 216
            Bitmap transformedBitmap;
            Matrix matrix = new Matrix();
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
217 218 219 220 221 222 223 224 225

            try {
                int orientation = getImageOrientation();
                if (orientation != 0) {
                    matrix.postRotate(orientation);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
226 227 228 229 230 231 232

            matrix.postScale(1, -1);

            transformedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.recycle();

            return transformedBitmap;
Gaëtan Renaudeau's avatar
WIP  
Gaëtan Renaudeau committed
233 234 235 236 237
        }

        protected abstract int getImageOrientation() throws IOException;
    }
}