2013-05-16 3 views
0

Я пробовал этот код, здесь преобразовал изображение в растровое изображение, а затем преобразовал его в оттенки серого. Но мы хотим, чтобы получить доступ к пиксельным значениям, но мы получаем все значения пикселей, как 0, пожалуйста, кто-нибудь помочь намКак получить положительные значения пикселей из растрового изображения в android

public Bitmap toGrayscale(Bitmap bmpOriginal) 
{   
    int width, height; 
    height = bmpOriginal.getHeight(); 
    width = bmpOriginal.getWidth(); 

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); 
    Canvas c = new Canvas(bmpGrayscale); 
    Paint paint = new Paint(); 
    ColorMatrix cm = new ColorMatrix(); 
    cm.setSaturation(0); 
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); 
    paint.setColorFilter(f); 
    c.drawBitmap(bmpOriginal, 0, 0, paint); 
    return bmpGrayscale; 
} 



@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 
     imageView = (ImageView) findViewById(R.id.imgView); 
     imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
     imageView.buildDrawingCache(); 
     Bitmap bmap = imageView.getDrawingCache(); 
     image=toGrayscale(bmap); 
     ImageView imageView1 = (ImageView) findViewById(R.id.imgView1); 
     imageView1.setImageBitmap(image); 
     pixels = new int[image.getWidth()*image.getHeight()]; 
     image.getPixels(pixels, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight()); 


     bu1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       text3.setText(String.valueOf(pixels[m++])); 

      } 
     }); 



    } 
+0

могли бы вы показать немного больше кода, пожалуйста? Где ваше растровое изображение «image» и ваше целое число «m» инициализировано? Вы получили некоторые ошибки? – Opiatefuchs

+0

Спасибо, теперь я добавил дополнительный код, здесь загружено изображение из галереи. И m инициализируется в том же классе, что и public ... – user2388616

ответ

-1

защищенный void onActivityResult (int requestCode, int resultCode, Intent data) { super.onActivityResult (requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 
      imageView = (ImageView) findViewById(R.id.imgView); 
     imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
     BitmapFactory.Options options=new BitmapFactory.Options(); 
     try 
     { 
     InputStream is = getContentResolver().openInputStream(selectedImage); 
     Bitmap bm = BitmapFactory.decodeStream(is,null,options); 
     int Height = bm.getHeight(); 
     int Width = bm.getWidth(); 
     int newHeight = 300; 
     int newWidth = 300; 
     float scaleWidth = ((float) newWidth)/Width; 
     float scaleHeight = ((float) newHeight)/Height; 
     Matrix matrix = new Matrix(); 
     matrix.postScale(scaleWidth, scaleHeight); 
     Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0,Width, Height, matrix, true); 
     image=toGrayscale(resizedBitmap); 
      imageView1 = (ImageView) findViewById(R.id.imgView1); 
      imageView1.setImageBitmap(image); 
     } 
     catch(Exception e) 
     { 

     } 

     pixels = new byte[image.getWidth()*image.getHeight()]; 
     pixels2 = new int[image.getWidth()*image.getHeight()]; 
     int k=0; 
     for (int i = 0; i < image.getHeight(); i++) 
     { 
      for (int j = 0; j < image.getWidth(); j++) 
      { 
        pixels[k]=(byte)(image.getPixel(j, i)); 
        pixels2[k]=unsignedToBytes(pixels[k]); 
        k++; 
      } 
     } 

}

0

Изменения Bitmap.Config.RGB_565 в Bitmap.Config.ARGB_8888.

+0

Спасибо, я пробовал использовать ARGB_8888 .. но все равно получаю все значения пикселей Zero .. – user2388616

+0

, если вы вызываете: Bitmap bw = toGrayscale (цветной); ваш BW Bitmap не должен иметь все пиксели нулевого размера, почему вы используете кэш-чертеж? – pskink

+0

Чтобы преобразовать изображение в растровое ... – user2388616

Смежные вопросы