Binarisation sous Android
Bonjour,
1) J'ai créé un projet de type Java. J'ai ajouté un jar externe qui est le framework Android et puis j'ai créé un package qui contient une activité MatrixImage qui hérite de Activity.
Voici son code source:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| package com.forma.jar;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import javax.swing.ImageIcon;
import android.app.Activity;
public class MatarixImage extends Activity {
int h;//nombre de lignes de la matrice de pixel
int w;//nombre de colonnes de la matrice de pixel
int[][] matrice;//matrice de pixel de l'image
int[][] mat;
Graphics g=null;
String pathImage;
BufferedImge bimage=null;
int a;
//------------------------Constructeur----------------------------------------------------
public MatrixImage(String pathImage)
{
this.pathImage=pathImage;
Image image=Toolkit.getDefaultToolkit().getImage(pathImage);
Image imgicon = new ImageIcon(image).getImage();
h=image.getHeight(null);
w=image.getWidth(null);
System.out.println("h "+h+" w "+w);
matrice=new int[w][h];
for(int i=0;i<w;i++)
for(int j=0;j<h;j++)
matrice[i][j]= 0;
bimage=creBuffereImage (image);
remplisageMatrice();
//afficheMatrice() ;
}
//--------------------------Création d'un Buffered Image-------------------------------------
public BufferedImage creBuffereImage (Image image)throws OutOfMemoryError
{
PixelGrabber pimage = new PixelGrabber(image, 0, 0, 1, 1,false);// pr�senter les pixels de l'image dans un tableau
int type = BufferedImage.TYPE_INT_RGB; //type de couleur de l'image
try// la methode grabPixels() doit etre dans un bloc try et catch
{
if(pimage.grabPixels() )//pimage.grabPixels() :return Boolean
type = BufferedImage.TYPE_INT_ARGB;
}
catch(Exception e){}
bimage = new BufferedImage(w, h, type);
g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
//---------Chercher et Ajuster les pixels colors en Noir -----------
public boolean blanc(int x, int y,BufferedImage buf)
{
int c = buf.getRGB(x,y);
int red = (c & 0x00FF0000) >> 16;
int green = (c & 0x0000FF00) >> 8;
int blue = c & 0x000000FF;
//int alpha = (c & 0xFF000000) >> 24;
//return new Color(red, blue, green);
if (red >128) //128
red=255;
else
red=0;
if (blue >128)
blue=255;
else
blue=0;
if (green >128)
green=255;
else
green=0;
if( red!=0 && blue!=0 && green!=0)
return true;
else
return false ;
}
//-----------------------Remplisage du matrice---------------------------------
public int remplisageMatrice()
{
int bh=bimage.getHeight();
int bw=bimage.getWidth();
for(int i=0;i<bw;i++)
for(int j=0;j<bh;j++)
{
int c = bimage.getRGB(i,j);
int red = (c & 0x00FF0000) >> 16;
int green = (c & 0x0000FF00) >> 8;
int blue = c & 0x000000FF;
//if(!blanc(i,j,bimage))
matrice[i][j]=(red+blue+green)/3;
a=matrice[i][j];
System.out.println(a);
//System.out.println("yasmine");
//
//System.out.println(matrice[i][j]);
}
return(a);
}
//---------------------Affichage du matrice de pixels------------------------------
public void afficheMatrice()
{
// remplisageMatrice();
for(int i=0;i<bimage.getHeight();i++)
{
for(int j=0;j<bimage.getWidth();j++)
{
System.out.print(matrice[j][i]);
System.out.print(" ");
/*
for (int k=0;k<bimage.getHeight();k++)
{ for(int e=0;e<bimage.getWidth();e++)
mat[k][e]=matrice[j][i];
}*/
}
System.out.println(" (L"+(i+1)+")");// numéro de la ligne de la matrice
}
//return(mat);
}
} |
2) J'ai créé un projet Android et puis j'ai fait propriété sur le projet > java build path et j'ai ajouté dans les projets le projet formation.jar.
Dans mon activité Main j'ai créé une instance de type MatrixImage et j'ai appelé la méthode remplissage de la classe MatrixImage.
Voici mon code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.forma.jarAndroid;
import android.util.Log;
import com.forma.jar.MatrixImage;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
//final String TAG = "MyActivity";
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MatrixImage matrice= new MatrixImage("C:\\Users\\hp\\workspace\\bimage.png");
TextView tv = new TextView(this);
tv.setText("notre image sous format matricielle" + matrice.remplisageMatrice());
setContentView(tv);
}
} |
Mon problème est que quand j'exécute, l'émulateur Android m'affiche un message
Citation:
the application has stopped unexpectedly, please try again
Comment puis-je résoudre cela ?
Merci d'avance pour votre aide.