Pages

Wednesday, August 19, 2015

Make Image Blur

Display Image into Blur effect


1) Edit MainActivity.Java in your project

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {
    private ImageView android;
    private ImageView resultimg;
    private TextView text;
    private TextView statusText;
    Uri source;
    Bitmap bitmapMaster;
    Canvas canvasMaster;
    RenderScript _rs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        android = (ImageView) findViewById(R.id.android1);
        resultimg = (ImageView) findViewById(R.id.resultimage);

        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.android1);
        Bitmap resultbm = blurImage(bm, 10);
        resultimg.setImageBitmap(resultbm);


    }

private Bitmap blurImage(Bitmap sentBitmap, int radius) {
        if (Build.VERSION.SDK_INT > 16) {
            Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
           
            RenderScript rs = RenderScript.create(this);
            Allocation input = Allocation.createFromBitmap(rs, sentBitmap, Allocation.MipmapControl.MIPMAP_NONE,
                    Allocation.USAGE_SCRIPT);
            Allocation output = Allocation.createTyped(rs, input.getType());
            ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
            script.setRadius(radius /* e.g. 3.f */);
            script.setInput(input);
            script.forEach(output);
            output.copyTo(bitmap);
            return bitmap;
        } else {
            return sentBitmap;
        }

    }
}

2)Edit activity_main.xml file 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/android1"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_marginBottom="10dp"
            android:scaleType="centerCrop"
            android:src="@drawable/android1" />

        <ImageView
            android:id="@+id/resultimage"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:scaleType="centerCrop" />

    </LinearLayout>
</ScrollView>

Output







No comments:

Post a Comment