Android 查看位图像素值

Android See bitmap pixel values

我正在学习图像处理。我已经阅读并研究过位图、像素和数组。我想知道图像的像素值。我的计划是将位图转换为数组并将其保存为文本文件。有没有其他方法可以查看图像的整个像素值数组?

是的,有一种方法可以将图像转换为文本。它实际上被称为字节数组。您可以这样做:

        // 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();

您也可以只获取字节数组,然后保存实际值,而不是将其转换为图像文件。要将字节数组转换为文件,请执行以下操作:

        // 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();