May 11, 2012

Example of EmbossMaskFilter

EmbossMaskFilter(float[] direction, float ambient, float specular, float blurRadius) create an emboss maskfilter.
  • direction: array of 3 scalars [x, y, z] specifying the direction of the light source
  • ambient: 0...1 amount of ambient light
  • specular: coefficient for specular highlights
  • blurRadius: amount to blur before applying lighting

Example of EmbossMaskFilter


package com.AndroidBitmapProcessing;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.EmbossMaskFilter;
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 ambientBar, specularBar, blurRadiusBar;
 TextView ambientText, specularText, blurRadiusText;
 Button doProcess;
 
 float ambientValue = 0.5f;
 float specularValue = 5.0f;
 float blurRadiusValue = 5.0f;
 
    /** 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_Emboss(bitmap_Source));
        
        ambientBar = (SeekBar)findViewById(R.id.ambientBar);
        specularBar = (SeekBar)findViewById(R.id.specularBar);
        blurRadiusBar = (SeekBar)findViewById(R.id.blurRadiusBar);
     ambientText = (TextView)findViewById(R.id.ambientText);
     specularText = (TextView)findViewById(R.id.specularText);
     blurRadiusText = (TextView)findViewById(R.id.blurRadiusText);
        
        doProcess = (Button)findViewById(R.id.doProcess);
        
        ambientBar.setOnSeekBarChangeListener(ambientBarChangeListener);
        specularBar.setOnSeekBarChangeListener(specularBarChangeListener);
        blurRadiusBar.setOnSeekBarChangeListener(blurRadiusBarChangeListener);
        
        doProcess.setOnClickListener(doProcessClickListener);
        
    }
    
    OnClickListener doProcessClickListener
    = new OnClickListener(){

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

  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
   ambientValue = progress/10f;
   ambientText.setText("ambient: " + String.valueOf(ambientValue));
  }

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

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

  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
   // TODO Auto-generated method stub
   specularValue = progress/10f;
   specularText.setText("specular: " + String.valueOf(specularValue));
  }

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

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

  @Override
  public void onProgressChanged(SeekBar seekBar, int progress,
    boolean fromUser) {
   blurRadiusValue = progress/10f;
   blurRadiusText.setText("blurRadius: " + String.valueOf(blurRadiusValue));
   
  }

  @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_Emboss(Bitmap src){
     int width = src.getWidth();
        int height = src.getHeight();

        EmbossMaskFilter embossMaskFilter;
        Paint paintEmboss = new Paint();
        
        Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
     Canvas canvas = new Canvas(dest);

     Bitmap alpha = src.extractAlpha();
          
     embossMaskFilter = new EmbossMaskFilter(
       new float[] { 1, 1, 1 },
       ambientValue, 
       specularValue, 
       blurRadiusValue);
     
     paintEmboss.setMaskFilter(embossMaskFilter);
     canvas.drawBitmap(alpha, 0, 0, paintEmboss);
     
        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/ambientBar"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:paddingLeft="10dp"
       android:paddingRight="10dp"
       android:max="10"
       android:progress="5"/>
   <TextView
       android:id="@+id/ambientText"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="ambient: 0.5"/>
   
   <SeekBar
       android:id="@+id/specularBar"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:paddingLeft="10dp"
       android:paddingRight="10dp"
       android:max="100"
       android:progress="50"/>
   <TextView
       android:id="@+id/specularText"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="specular: 5"/>
   
   <SeekBar
       android:id="@+id/blurRadiusBar"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:paddingLeft="10dp"
       android:paddingRight="10dp"
       android:max="100"
       android:progress="50"/>
   <TextView
       android:id="@+id/blurRadiusText"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="blurRadius: 5"/>
   
   <Button
       android:id="@+id/doProcess"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="Do Process"/>
     </LinearLayout>
 </ScrollView>
</LinearLayout>


1 comment:

  1. Thanks a lot!
    It is also very helpful to find nice parameters for emboss :)

    ReplyDelete

Infolinks In Text Ads