我编写了以下代码来使图像变灰。在早期的项目中,我对 JNI 有一些经验,现在我也想尝试一下 Renderscript。所以,我写了以下代码:
// MainActivity.java
public class MainActivity extends AppCompatActivity {
private Bitmap mBitmapIn;
private Bitmap mBitmapOut;
private ImageView mImageView;
private Allocation mInAllocation;
private Allocation mOutAllocation;
private Button mButton;
private ScriptC_gray mScript;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton =(Button) findViewById(R.id.button);
// Initialize UI
mBitmapIn = loadBitmap(R.drawable.data);
mBitmapOut = Bitmap.createBitmap
(
mBitmapIn.getWidth(),
mBitmapIn.getHeight(),
mBitmapIn.getConfig()
);
mImageView = (ImageView) findViewById(R.id.imageView);
mImageView.setImageBitmap(mBitmapIn);
// Create renderScript
RenderScript rs = RenderScript.create(this);
// Allocate buffers
mInAllocation = Allocation.createFromBitmap(rs, mBitmapIn);
mOutAllocation = Allocation.createFromBitmap(rs, mBitmapOut);
mScript = new ScriptC_gray(rs); // Load script
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Invoke renderScript kernel and update imageView
mScript.forEach_gray(mInAllocation, mOutAllocation);
// Copy to bitmap and invalidate image view
mOutAllocation.copyTo(mBitmapOut);
mImageView.setImageBitmap(mBitmapOut);
}
});
}
/**
* Helper to load Bitmap from resource
*/
private Bitmap loadBitmap(int resource) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeResource(getResources(), resource, options);
}
}
如您所见,我加载图像,通过创建 IN/OUT 分配、应用内核函数并将结果放到屏幕上来准备整个渲染脚本。
gray.rs 文件如下所示:
// gray.rs
#pragma version(1)
#pragma rs java_package_name(com.celik.abdullah.grayscale)
#pragma rs_fp_relaxed
const float4 weight = {0.299f, 0.587f, 0.114f, 0.0f}; // for grayscale
/*
* RenderScript kernel that performs grayscale manipulation
*/
uchar4 __attribute__((kernel)) gray(uchar4 in)
{
float4 inF = rsUnpackColor8888(in);
float4 outF = (float4){ dot(inF, weight) };
return rsPackColorTo8888(outF);
}
当我运行该项目时,会发生以下情况:
结果 :
因此,在我单击按钮并开始灰显过程后,ImageView 变为空白。为什么 ?我找不到我的错误。我按照官方文档中的步骤进行操作,但也许我错过了一些东西