Skip to content

Commit

Permalink
RenderScript in AndroidX is making things crash; disable for now!
Browse files Browse the repository at this point in the history
- use a simple method for NV21 image conversation until RenderScriptX stops being crashy
  • Loading branch information
n8fr8 committed Feb 6, 2019
1 parent 4b808e6 commit 4aa51f7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.0'
ext.kotlin_version = '1.3.20'
repositories {
google()
jcenter()
Expand Down Expand Up @@ -91,8 +91,9 @@ android {
}
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
renderscriptTargetApi 16
renderscriptSupportModeEnabled true

// renderscriptTargetApi 16
// renderscriptSupportModeEnabled true

ndk {
abiFilters "armeabi", "armeabi-v7a", "x86"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.util.concurrent.TimeUnit;

import androidx.annotation.NonNull;
import androidx.renderscript.RenderScript;

import io.github.silvaren.easyrs.tools.Nv21Image;

Expand Down Expand Up @@ -107,7 +106,7 @@ public class CameraViewHolder {
private File videoFile;

//for managing bitmap processing
private RenderScript renderScript;
//private RenderScript renderScript;

private ServiceConnection mConnection = new ServiceConnection() {

Expand All @@ -128,12 +127,11 @@ public CameraViewHolder(Activity context, CameraView cameraView) {
//super(context);
this.context = context;
this.cameraView = cameraView;
this.renderScript = RenderScript.create(context); // where context can be your activity, application, etc.
//this.renderScript = RenderScript.create(context); // where context can be your activity, application, etc.

prefs = new PreferenceManager(context);

task = new MotionDetector(
renderScript,
updateHandler,
motionSensitivity);

Expand Down Expand Up @@ -287,7 +285,8 @@ public void updateCamera ()
private void recordNewFrame (byte[] data, int width, int height, int rotationDegrees)
{

Bitmap bitmap = Nv21Image.nv21ToBitmap(renderScript, data, width, height);
Bitmap bitmap = MotionDetector.convertImage(data, width, height);
//Nv21Image.nv21ToBitmap(renderScript, data, width, height);

bitmap = Bitmap.createBitmap(bitmap,0,0,width,height,mtxVideoRotate,true);

Expand Down
26 changes: 20 additions & 6 deletions src/main/java/org/havenapp/main/sensors/motion/MotionDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.os.Handler;


import org.havenapp.main.sensors.media.ImageCodec;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import androidx.renderscript.RenderScript;
import io.github.silvaren.easyrs.tools.Nv21Image;

/**
* Task doing all image processing in backgrounds,
Expand All @@ -41,7 +44,7 @@ public class MotionDetector {

private IMotionDetector detector;

private RenderScript renderScript;
//private RenderScript renderScript;

private int detectColor = Color.YELLOW;

Expand All @@ -57,10 +60,10 @@ public void addListener(MotionListener listener) {
}

public MotionDetector(
RenderScript renderScript,

Handler updateHandler,
int motionSensitivity) {
this.renderScript = renderScript;
// this.renderScript = renderScript;
this.handler = updateHandler;
this.motionSensitivity = motionSensitivity;
detector = new LuminanceMotionDetector();
Expand Down Expand Up @@ -128,7 +131,8 @@ public void detect(byte[] rawOldPic,
Bitmap newBitmap
= Bitmap.createBitmap(Bitmap.createBitmap(newPic, width, height, Bitmap.Config.ARGB_4444), 0, 0, width, height, mtx, true);

Bitmap rawBitmap = Bitmap.createBitmap(Nv21Image.nv21ToBitmap(renderScript, rawNewPic, width, height),0,0,width,height,mtx,true);
Bitmap rawBitmap = convertImage(rawNewPic,width,height);
//Bitmap.createBitmap(Nv21Image.nv21ToBitmap(renderScript, rawNewPic, width, height),0,0,width,height,mtx,true);

handler.post(() -> {
for (MotionListener listener : listeners) {
Expand Down Expand Up @@ -161,5 +165,15 @@ public void detect(byte[] rawOldPic,

}

public static Bitmap convertImage (byte[] nv21bytearray, int width, int height)
{
YuvImage yuvImage = new YuvImage(nv21bytearray, ImageFormat.NV21, width, height, null);
ByteArrayOutputStream os = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, os);
byte[] jpegByteArray = os.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(jpegByteArray, 0, jpegByteArray.length);
return bitmap;
}


}

0 comments on commit 4aa51f7

Please sign in to comment.