May 10, 2012

Create blur edge on bitmap, using BlurMaskFilter.

The android.graphics.BlurMaskFilter class takes a mask, and blurs its edge by the specified radius. This example show how to make blur bitmap inner, and blur white outer edge.

Create blur edge on bitmap, using BlurMaskFilter.


package com.AndroidBitmapProcessing;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class AndroidBitmapProcessingActivity extends Activity {
 
 ImageView imageView_Source, imageAfter;
 Bitmap bitmap_Source, bitmap_Dest;
 
 SeekBar blurBar;
 TextView blurText;
 Button doProcess;
 
 int blurValue = 1;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView_Source = (ImageView)findViewById(R.id.source);
        imageAfter = (ImageView)findViewById(R.id.imageAfter);

        //bitmap_Source = BitmapFactory.decodeResource(getResources(), R.drawable.testpicture);
        bitmap_Source = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        imageAfter.setImageBitmap(processingBitmap_Blur(bitmap_Source));
        
        blurBar = (SeekBar)findViewById(R.id.blurBar);
        blurText = (TextView)findViewById(R.id.blurText);
        doProcess = (Button)findViewById(R.id.doProcess);
        
        blurBar.setOnSeekBarChangeListener(blurBarChangeListener);
        doProcess.setOnClickListener(doProcessClickListener);
        
    }
    
    OnClickListener doProcessClickListener
    = new OnClickListener(){

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   imageAfter.setImageBitmap(processingBitmap_Blur(bitmap_Source));
  }};
    
    OnSeekBarChangeListener blurBarChangeListener
    = new OnSeekBarChangeListener(){

  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
   blurValue = progress+1;
   blurText.setText(String.valueOf(blurValue));
  }

  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
   // TODO Auto-generated method stub
   
  }
     
    };

    private Bitmap processingBitmap_Blur(Bitmap src){
     int width = src.getWidth();
        int height = src.getHeight();
        
     BlurMaskFilter blurMaskFilter;
     Paint paintBlur = new Paint();
     
     Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
     Canvas canvas = new Canvas(dest); 
     
     //Create background in White
     Bitmap alpha = src.extractAlpha();
     paintBlur.setColor(0xFFFFFFFF);
     canvas.drawBitmap(alpha, 0, 0, paintBlur);
     
     //Create outer blur, in White
     blurMaskFilter = new BlurMaskFilter(blurValue, BlurMaskFilter.Blur.OUTER);
     paintBlur.setMaskFilter(blurMaskFilter);
     canvas.drawBitmap(alpha, 0, 0, paintBlur);
     
     //Create inner blur
     blurMaskFilter = new BlurMaskFilter(blurValue, BlurMaskFilter.Blur.INNER);
     paintBlur.setMaskFilter(blurMaskFilter);
     canvas.drawBitmap(src, 0, 0, paintBlur);
     
     
     
     return dest;
    }

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <ScrollView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout 
            android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:orientation="vertical" >
         <TextView
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="Original" />
      <ImageView
          android:id="@+id/source" 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" 
          android:src="@drawable/ic_launcher"/>
      <TextView
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="Result" />
      <ImageView
          android:id="@+id/imageAfter" 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
   <SeekBar
       android:id="@+id/blurBar"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:max="99"
       android:progress="0"/>
   <TextView
       android:id="@+id/blurText"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="1"/>
   <Button
       android:id="@+id/doProcess"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="Do Process"/>
     </LinearLayout>
 </ScrollView>
</LinearLayout>


1 comment:

Infolinks In Text Ads