package com.example.androidimageview;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicConvolve3x3;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
 ImageView imageA1, imageA2, imageB1, imageB2, imageC1, imageC2;
 Bitmap bitmapOriginal, bitmapBlur, bitmapSharpen;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  imageA1 = (ImageView) findViewById(R.id.imagea1);
  imageA2 = (ImageView) findViewById(R.id.imagea2);
  imageB1 = (ImageView) findViewById(R.id.imageb1);
  imageB2 = (ImageView) findViewById(R.id.imageb2);
  imageC1 = (ImageView) findViewById(R.id.imagec1);
  imageC2 = (ImageView) findViewById(R.id.imagec2);
  bitmapOriginal = BitmapFactory.decodeResource(getResources(),
    R.drawable.ic_launcher);
  imageA1.setImageBitmap(bitmapOriginal);
  imageA2.setImageBitmap(bitmapOriginal);
  
  // create sharpen bitmap from blur bitmap
  bitmapSharpen = createBitmap_convolve(bitmapOriginal, matrix_sharpen);
  imageB1.setImageBitmap(bitmapSharpen);
  imageB2.setImageBitmap(bitmapSharpen);
  // create blur bitmap from original bitmap
  bitmapBlur = createBitmap_convolve(bitmapOriginal, matrix_blur);
  imageC1.setImageBitmap(bitmapBlur);
  imageC2.setImageBitmap(bitmapBlur);
 }
 
 float[] matrix_blur = 
  { 1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f,
    1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f,
    1.0f/9.0f, 1.0f/9.0f, 1.0f/9.0f};
 
 float[] matrix_sharpen = 
  { 0, -1, 0,
    -1, 5, -1,
    0, -1, 0};
 private Bitmap createBitmap_convolve(Bitmap src, float[] coefficients) {
  Bitmap result = Bitmap.createBitmap(src.getWidth(),
    src.getHeight(), src.getConfig());
  RenderScript renderScript = RenderScript.create(this);
  Allocation input = Allocation.createFromBitmap(renderScript, src);
  Allocation output = Allocation.createFromBitmap(renderScript, result);
  ScriptIntrinsicConvolve3x3 convolution = ScriptIntrinsicConvolve3x3
    .create(renderScript, Element.U8_4(renderScript));
  convolution.setInput(input);
  convolution.setCoefficients(coefficients);
  convolution.forEach(output);
  output.copyTo(result);
  renderScript.destroy();
  return result;
 }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidimageview.MainActivity" >
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
            <ImageView
                android:id="@+id/imagea1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/imagea2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
            <ImageView
                android:id="@+id/imageb1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/imageb2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
            <ImageView
                android:id="@+id/imagec1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:id="@+id/imagec2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
Next:
- interactive exercise of ScriptIntrinsicConvolve3x3

 
1 comment:
Thanks for the code!
Post a Comment