import org.opencv.core.Mat; //导入方法依赖的package包/类
private void example() {
RenderScript mRS = RenderScript.create(this);
// Loads input image
Bitmap inputBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.houseimage);
// Puts input image inside an OpenCV mat
Mat inputMat = new Mat();
Utils.bitmapToMat(inputBitmap, inputMat);
Mat outputMat = new Mat(inputMat.size(), inputMat.type());
// Testing bitmap, used to test that the OpenCV mat actually has bitmap data inside
Bitmap initialBitmap = Bitmap.createBitmap(inputMat.width(), inputMat.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(inputMat, initialBitmap);
// Retrieve OpenCV mat data address
long inputMatDataAddress = inputMat.dataAddr();
long outputMatDataAddress = outputMat.dataAddr();
// Creates a RS type that matches the input mat one.
Element element = Element.RGBA_8888(mRS);
Type.Builder tb = new Type.Builder(mRS, element);
tb.setX(inputMat.width());
tb.setY(inputMat.height());
Type inputMatType = tb.create();
// Creates a RenderScript allocation that uses directly the OpenCV input mat address
Allocation inputAllocation = createTypedAllocationWithDataPointer(mRS, inputMatType, inputMatDataAddress);
Allocation outputAllocation = createTypedAllocationWithDataPointer(mRS, inputMatType, outputMatDataAddress);
// Define a simple convolve script
// Note: here, ANY kernel can be applied!
ScriptIntrinsicConvolve3x3 convolve3x3 = ScriptIntrinsicConvolve3x3.create(mRS, element);
float convolveCoefficients[] = new float[9];
convolveCoefficients[0] = 1;
convolveCoefficients[2] = 1;
convolveCoefficients[5] = 1;
convolveCoefficients[6] = 1;
convolveCoefficients[8] = 1;
convolve3x3.setCoefficients(convolveCoefficients);
convolve3x3.setInput(inputAllocation);
convolve3x3.forEach(outputAllocation);
mRS.finish();
// Converts the result to a bitmap
Bitmap cvOutputBitmap = Bitmap.createBitmap(outputMat.width(), outputMat.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(outputMat, cvOutputBitmap);
// Testing bitmap, used to test the RenderScript ouput allocation contents
// Note: it is placed here because the copyTo function clears the input buffer
Bitmap rsOutputBitmap = Bitmap.createBitmap(outputMat.width(), outputMat.height(), Bitmap.Config.ARGB_8888);
outputAllocation.copyTo(rsOutputBitmap);
// Testing bitmap, used to test that RenderScript input allocation pointed to the OpenCV mat
// Note: it is placed here because the copyTo function clears the input buffer
Bitmap rsInitialBitmap = Bitmap.createBitmap(inputMat.width(), inputMat.height(), Bitmap.Config.ARGB_8888);
inputAllocation.copyTo(rsInitialBitmap);
// Display input and output
ImageView originalImageIV = (ImageView) findViewById(R.id.imageView);
ImageView inputRSImageIV = (ImageView) findViewById(R.id.imageView2);
ImageView outputRSImageIV = (ImageView) findViewById(R.id.imageView3);
ImageView outputCVIV = (ImageView) findViewById(R.id.imageView4);
originalImageIV.setImageBitmap(initialBitmap);
inputRSImageIV.setImageBitmap(rsInitialBitmap);
outputRSImageIV.setImageBitmap(rsOutputBitmap);
outputCVIV.setImageBitmap(cvOutputBitmap);
}