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
| package com.test.Gray_Bitmap;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class toGrayScale extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/////////////////////////////
int width, height;
Bitmap bmpOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.gris1);
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
int[] pix =new int [width * height];
bmpOriginal.getPixels(pix, 0, width, 0, 0, width, height);
int somme_pix=0;
for (int x = 0; x < width * height; x++)
{
somme_pix= somme_pix+pix[x];
}
int seuil= (somme_pix/(width*height)*(-1));
TextView tv=new TextView(this);
tv.setText("somme des pixels= "+somme_pix+"width= "+width+"height= "+height+ "seuil= "+seuil);
setContentView(tv);
byte[] bpix=new byte [width * height];
for (int x = 0; x < (width * height); x++)
{
if ( pix[x]< seuil )
pix[x]=1;
else
pix[x]=0;
}
Bitmap bmpCopy=bmpOriginal.copy(Bitmap.Config.ALPHA_8, false);
bmpCopy.setPixels(pix, 0, width, 0, 0, width, height) ;
}
} |
Partager