0

我正在尝试使图像模糊,但是在运行应用程序时出现空点异常错误是我的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.union.test44.MainActivity"
tools:showIn="@layout/activity_main">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/thumb5"/>

我的java代码是

Context context;
ImageView imageview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview = (ImageView)findViewById(R.id.imageView);

BitmapDrawable drawable = (BitmapDrawable) imageview.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Bitmap blurred = blurRenderScript(bitmap, 24);//second parametre is radius
imageview.setImageBitmap(blurred);

}

@SuppressLint("NewApi")
private Bitmap blurRenderScript(Bitmap smallBitmap, int radius) {

try {
    smallBitmap = RGB565toARGB888(smallBitmap);
} catch (Exception e) {
    e.printStackTrace();
}


Bitmap bitmap = Bitmap.createBitmap(
        smallBitmap.getWidth(), smallBitmap.getHeight(),
        Bitmap.Config.ARGB_8888);

RenderScript renderScript = RenderScript.create(context);

Allocation blurInput = Allocation.createFromBitmap(renderScript, smallBitmap);
Allocation blurOutput = Allocation.createFromBitmap(renderScript, bitmap);

ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
        Element.U8_4(renderScript));
blur.setInput(blurInput);
blur.setRadius(radius); // radius must be 0 < r <= 25
blur.forEach(blurOutput);

blurOutput.copyTo(bitmap);
renderScript.destroy();

return bitmap;

}

private Bitmap RGB565toARGB888(Bitmap img) throws Exception {
int numPixels = img.getWidth() * img.getHeight();
int[] pixels = new int[numPixels];

//Get JPEG pixels.  Each int is the color values for one pixel.
img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());

//Create a Bitmap of the appropriate format.
Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.ARGB_8888);

//Set RGB pixels.
result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight());
return result;
}
4

2 回答 2

1

你试过这样吗:

Bitmap attachedbitmap= ((BitmapDrawable)imageview.getDrawable()).getBitmap();

于 2016-07-05T00:06:51.103 回答
0

你最好直接使用:

// Your ImageView
ImageView originalImageView = (ImageView) findViewById(R.id.imageView);

// Input image (the drawable)
Bitmap inputImage = BitmapFactory.decodeResource(getResources(), R.drawable.thumb5);
int inputWidth = inputImage.getWidth();
int inputHeight = inputImage.getHeight();

Allocation inputAllocation = Allocation.createFromBitmap(mRS, inputImage);    

// Generate the output allocation
Type.Builder tbOutput = new Type.Builder(mRS, Element.RGBA_8888(mRS));
tbOutput.setX(inputWidth);
tbOutput.setY(inputHeight);
Allocation outputAllocation = Allocation.createTyped(mRS, tbOutput.create());

// Go on with the processing :)
...

// Then, set the bitmap
Bitmap outputBitmap = Bitmap.createBitmap(inputWidth, inputHeight, Bitmap.Config.ARGB_8888);

outputAllocation.copyTo(outputBitmap);

// Copy bitmap to ImageView    
originalImageView.setImageBitmap(outputBitmap);

参考:RenderScript:Android 上的并行计算,最简单的方法

于 2016-07-05T00:04:01.430 回答