单源RenderScript Google示例不起作用

时间:2017-12-29 08:19:00

标签: android renderscript

我正在尝试将单源RenderScript示例编写为Google's guide。但是,它似乎不起作用。

以下是我的配置和源代码。

的build.gradle

compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
    applicationId "com.aquarius.rsdemo"
    minSdkVersion 24
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    renderscriptTargetApi 24
}

gray.rs

#pragma version(1)
#pragma rs java_package_name(com.aquarius.rsdemo)

static const float4 weight = {0.299f, 0.587f, 0.114f, 0.0f};

uchar4 RS_KERNEL invert(uchar4 in, uint32_t x, uint32_t y) {
  uchar4 out = in;
  out.r = 255 - in.r;
  out.g = 255 - in.g;
  out.b = 255 - in.b;
  return out;
}

uchar4 RS_KERNEL greyscale(uchar4 in) {
  const float4 inF = rsUnpackColor8888(in);
  const float4 outF = (float4){ dot(inF, weight) };
  return rsPackColorTo8888(outF);
}

void process(rs_allocation inputImage, rs_allocation outputImage) {
  const uint32_t imageWidth = rsAllocationGetDimX(inputImage);
  const uint32_t imageHeight = rsAllocationGetDimY(inputImage);
  rs_allocation tmp = rsCreateAllocation_uchar4(imageWidth, imageHeight);
  rsForEach(invert, inputImage, tmp);
  rsForEach(greyscale, tmp, outputImage);
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    ImageView img;
    Bitmap src, dst;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageView) findViewById(R.id.img);
        src = BitmapFactory.decodeResource(getResources(), R.drawable.image);
        dst = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        img.setImageBitmap(src);
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RenderScript RS = RenderScript.create(getApplication());
                ScriptC_gray script = new ScriptC_gray(RS);
                Allocation inputAllocation = Allocation.createFromBitmap(RS, src);
                Allocation outputAllocation = Allocation.createTyped(
                        RS, inputAllocation.getType(),
                        Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT);
                script.invoke_process(inputAllocation, outputAllocation);
                outputAllocation.copyTo(dst);
                img.setImageBitmap(dst);
            }
        });

    }
}

我想我做了一些像教程一样的事情。但是,点击按钮后,ImageView并没有显示任何内容。

0 个答案:

没有答案