Jun 4, 2012

Image Processing - inverse bitmap to generate film like picture

This article modify the last article "Image Processing with open JPG file dialog", inverse color from the original bitmap to generate film like picture.

Inverse bitmap to generate film like picture


Modify the AndroidImageProcessingActivity.java from last article "Image Processing with open JPG file dialog". Other files keep no change.

package com.AndroidImageProcessing;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class AndroidImageProcessingActivity extends Activity {
 
 //for Load JPG dialog
 Button buttonUp;
 static final int LOAD_DIALOG_ID = 0;
 TextView textFolder;
 ListView dialog_ListView;
 private List<String> fileList = new ArrayList<String>();
 
 File root;
 File curFolder;
 
 ImageView imageSource, imageAfter;
 Bitmap bitmap_Source;
 ProgressBar progressBar;
 
 private Handler handler;
 Bitmap afterProcess;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageSource = (ImageView)findViewById(R.id.imageSource);
        imageAfter = (ImageView)findViewById(R.id.imageAfter);
        progressBar = (ProgressBar)findViewById(R.id.progressBar);
        progressBar.setVisibility(View.GONE);
        
        bitmap_Source = BitmapFactory.decodeResource(getResources(), R.drawable.testpicture);

        handler = new Handler();
        //performImageProcessing(bitmap_Source);
        
        root = new File(Environment
          .getExternalStorageDirectory()
          .getAbsolutePath());
        
        curFolder = root;
    }
    
    private void performImageProcessing(Bitmap bmSrc){
     imageSource.setImageBitmap(bmSrc);
     progressBar.setVisibility(View.VISIBLE);
  imageAfter.setVisibility(View.GONE);
     StratBackgroundProcess(bmSrc);
    }
    
    private void StratBackgroundProcess(final Bitmap bm_src){
     
     Runnable runnable = new Runnable(){

   @Override
   public void run() {
    //afterProcess = processingBitmap(bm_src, kernal_blur);
    afterProcess = InverseBitmap(bm_src);
    
    handler.post(new Runnable(){

     @Override
     public void run() {
      progressBar.setVisibility(View.GONE);
      imageAfter.setVisibility(View.VISIBLE);
      imageAfter.setImageBitmap(afterProcess);
     }
     
    });
   }
     };
     new Thread(runnable).start();
    }
    
    private Bitmap InverseBitmap(Bitmap src){
     Bitmap dest = Bitmap.createBitmap(
       src.getWidth(), src.getHeight(), src.getConfig());
     
     for(int i = 0; i < src.getWidth(); i++){
      for(int j = 0; j < src.getHeight(); j++){

       dest.setPixel(i, j, Color.argb(
         Color.alpha(src.getPixel(i, j)), 
         255 - Color.red(src.getPixel(i, j)), 
         255 - Color.green(src.getPixel(i, j)), 
         255 - Color.blue(src.getPixel(i, j))));
      } 
     }
     
     return dest;
    }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  menu.add(0, 0, 0, "Load jpg");
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch(item.getItemId()){
  case (0):
   showDialog(LOAD_DIALOG_ID);
   break;
  }
  return true;
 }

 @Override
 protected Dialog onCreateDialog(int id) {

  Dialog dialog = null;
  
  switch(id) {
     case LOAD_DIALOG_ID:
      dialog = new Dialog(AndroidImageProcessingActivity.this);
      dialog.setContentView(R.layout.load_dialog);
      dialog.setTitle("Load JPG");
      
      dialog.setCancelable(true);
      dialog.setCanceledOnTouchOutside(true);
      
      textFolder = (TextView)dialog.findViewById(R.id.folder);

      buttonUp = (Button)dialog.findViewById(R.id.up);
      buttonUp.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     ListDir(curFolder.getParentFile());
    }});

      //Prepare ListView in dialog
      dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);

      dialog_ListView.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {
     
     File selected = new File(fileList.get(position));
     if(selected.isDirectory()){
      ListDir(selected); 
     }else {
      Toast.makeText(AndroidImageProcessingActivity.this,
        selected.toString() + " selected",
        Toast.LENGTH_LONG).show();
      dismissDialog(LOAD_DIALOG_ID);
      
      Bitmap bm = BitmapFactory.decodeFile(selected.getAbsolutePath());
      imageSource.setImageBitmap(bm);
            performImageProcessing(bm);
        }
     
    }});
      
         break;
     }

  return dialog;
 }

 @Override
 protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) {
  // TODO Auto-generated method stub
  super.onPrepareDialog(id, dialog, bundle);

  switch(id) {
     case LOAD_DIALOG_ID:
      ListDir(curFolder);
         break;
     }
  
 }
 
 void ListDir(File f){

  if(f.equals(root)){
   buttonUp.setEnabled(false);
  }else{
   buttonUp.setEnabled(true);
  }
  
  curFolder = f;
  textFolder.setText(f.getPath());
  
  File[] files = f.listFiles();
  fileList.clear();
  for (File file : files){
   
   if(file.isDirectory()){
    fileList.add(file.getPath());
   }else{
    Uri selectedUri = Uri.fromFile(file);
    String fileExtension
     = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
    if(fileExtension.equalsIgnoreCase("jpg")){
     fileList.add(file.getPath());
    }
   }
   
  }
       
  ArrayAdapter<String> directoryList
   = new ArrayAdapter<String>(this,
     android.R.layout.simple_list_item_1, fileList);
  dialog_ListView.setAdapter(directoryList);
 }

}


Read it: My "Image processing" series.


No comments:

Post a Comment

Infolinks In Text Ads