简体   繁体   中英

Android See bitmap pixel values

I'm on a image processing study. I've read and researched about bitmaps, pixels and arrays. I want to know the pixel values of the image. My plan is to convert the bitmap to an array and save it as a text file. Is there any other way to see the whole array of pixel values of an image?

Yes, there is a way to convert an image to text. It is actually called a byte array. You can do this like so:

        // Convert bitmap to byte array
        Bitmap bitmap = bitmapImage;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
        byte[] bitmapdata = bos.toByteArray();

You can also just take the byte array and then save the actual values instead of turning it into an image file. To turn the byte array into a file, do this:

        // Create a file to write bitmap data
        File f = new File(getApplicationContext().getCacheDir(), "image.png");
        f.createNewFile();

        // INSERT ABOVE CODE HERE (BITMAP TO BYTE ARRAY CONVERSION)

        // Write the bytes in file
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM