1

What i want to do is to read some image in android and convert each pixel in my image to RGB values , and here is the code that i have found :

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button test=(Button)findViewById(R.id.button1);
        test.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                BufferedImage bi=ImageIO.read(new File("C:\\images\\Sunset.jpg"));
                int[] pixel;

                for (int y = 0; y < bi.getHeight(); y++) {
                    for (int x = 0; x < bi.getWidth(); x++) {
                        pixel = bi.getRaster().getPixel(x, y, new int[3]);
                        System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (bi.getWidth() * y + x));
                    }
                }
            }
        });

    }
}

But i have problems in importing some packages like this :

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

And i don't know how to download them or which is the best way to resolve this problem.. Any help??

sereen
  • 75
  • 12
  • you should use this: http://developer.android.com/reference/android/graphics/BitmapFactory.html BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options); selected_photo.setImageBitmap(bitmap); – ant Sep 07 '14 at 07:43
  • possible duplicate of [Can't load javax.imageio.ImageIO lib to Eclipse](http://stackoverflow.com/questions/16693011/cant-load-javax-imageio-imageio-lib-to-eclipse) – Harald K Sep 07 '14 at 17:29

2 Answers2

0

BufferedImage or ImageIO are not available on android.

Instead of BufferedImage use Bitmap, take a look at the getPixels function.

Instead of ImageIO use BitmapFactory, the function you need is decodeFile


BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, opts);

int pixels[] = new int[bitmap.getWidth()*bitmap.getHeight()];

bitmap.getPixels(pixels,0,0,0,0,bitmap.getWidth(),bitmap.getHeight());
Marco Martinelli
  • 892
  • 2
  • 14
  • 27
0

first one create Bitmap from File.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);

next you can getPixel() from you bitmap

for (int y = 0; y < bitmap.getHeight(); y++) {
    for (int x = 0; x < bitmap.getWidth(); x++) {
        android.util.Log.v("TAG", bitmap.getPixel(x,y));
    }
}
ant
  • 397
  • 1
  • 5
  • 19
  • but how should i configure photopath? – sereen Sep 07 '14 at 08:12
  • photoPath is a String, the same as filePath in my answer. Also please note that is more efficient to read all the pixels at once with readPixels as in my example. – Marco Martinelli Sep 07 '14 at 08:37
  • 1
    String photopath = android.os.Environment.getExternalStorageDirectory() + "your_photo.jpg"; – ant Sep 07 '14 at 08:43
  • ok but i want to run it on emulator and my image is exist in C drive like this : C:\\images\\download.jpg , will it work? @user2038145 – sereen Sep 07 '14 at 08:45
  • this will help you http://www.androiddevelopment.org/2008/11/11/how-to-create-and-use-the-sd-card-with-the-android-emulator/ – ant Sep 07 '14 at 08:48
  • well i am not trying to read it from sd card .. i want to read it from C drive on my computer , will it work? – sereen Sep 07 '14 at 08:50
  • no, emulator can access only its own sdcard. so you must create it first – ant Sep 07 '14 at 08:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60771/discussion-between-sereen-and-user2038145). – sereen Sep 07 '14 at 09:00
  • 1
    Use adb push or DDMS in Eclipse – Marco Martinelli Sep 07 '14 at 09:00