将字节数组复制到分配中的Android渲染脚本问题

时间:2018-12-26 13:42:49

标签: android image-processing renderscript android-renderscript

我是Renderscript的新手,我的应用程序每次在此行崩溃:

inAllocation.copyFromUnchecked(copyOfRange(nv21ByteArray, 0, size-1));

我想要做的是将我从相机获得的YUV图像(nv21ByteArray)转换为黑白图像(所以我只需要Y值)。 这是Java代码:

package org.rwca.anthe.robocup15;

import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v8.renderscript.Type;
import android.widget.ImageView;
import android.widget.Toast;

import org.jetbrains.annotations.NotNull;

import io.fotoapparat.Fotoapparat;
import io.fotoapparat.error.CameraErrorListener;
import io.fotoapparat.exception.camera.CameraException;
import io.fotoapparat.parameter.ScaleType;
import io.fotoapparat.preview.Frame;
import io.fotoapparat.preview.FrameProcessor;
import io.fotoapparat.view.CameraView;

import static io.fotoapparat.log.LoggersKt.fileLogger;
import static io.fotoapparat.log.LoggersKt.logcat;
import static io.fotoapparat.log.LoggersKt.loggers;
import static io.fotoapparat.selector.LensPositionSelectorsKt.back;
import static java.util.Arrays.copyOfRange;

public class MainActivity extends AppCompatActivity {

    private CameraView cameraView;
    private ImageView imageView;
    private Fotoapparat fotoapparat;

    public byte[] nv21ByteArray;
    public int size = 960 * 1280;
    public int thresholdValue;

    public RenderScript rs;
    public Allocation inAllocation, outAllocation;
    public ScriptC_imageProc mScript;

    @NonNull
    private Fotoapparat createFotoapparat() {
        return Fotoapparat
                .with(this)
                .into(cameraView)
                .previewScaleType(ScaleType.CenterCrop)
                .lensPosition(back())
                .frameProcessor(new SampleFrameProcessor())
                .logger(loggers(
                        logcat(),
                        fileLogger(this)
                ))
                .cameraErrorCallback(new CameraErrorListener() {
                    @Override
                    public void onError(CameraException e) {
                        Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();;
                    }
                })
                .build();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraView = findViewById(R.id.cameraView);
        imageView = findViewById(R.id.imageView);
        fotoapparat = createFotoapparat();

        rs = RenderScript.create(this);
        mScript = new ScriptC_imageProc(rs);
        Type t = Type.createX(rs, Element.U8(rs), size);
        inAllocation = Allocation.createSized(rs, Element.U8(rs), size);
        outAllocation = Allocation.createTyped(rs, t);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        fotoapparat.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        fotoapparat.stop();
    }

    private class SampleFrameProcessor implements FrameProcessor {
        @Override
        public void process(@NotNull Frame frame) {

            nv21ByteArray = frame.getImage();

            inAllocation.copyFromUnchecked(copyOfRange(nv21ByteArray, 0, size-1));

            mScript.set_thresholdValue(thresholdValue);
            mScript.forEach_root(inAllocation, outAllocation);



            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                }
            });
        }
    }
}

这是我的渲染脚本文件:

#pragma version(1)
#pragma rs java_package_name(org.rwca.anthe.robocup15)

#include "rs_core.rsh"
#include "rs_time.rsh"

int thresholdValue;

uchar __attribute__((kernel)) root(uchar in, uint32_t x) {
    //rsDebug("Called root", rsUptimeMillis());
}

我在做什么错了?

预先感谢您, 安东

0 个答案:

没有答案