首先感谢您抽出时间。我有一个 jar 库,它将作为库包含在我的 Android 应用程序中。
除了其他功能之外,这个 jar 还能够从 jpg 图像中获取 RGB 值。这在我的 java 应用程序中完美运行,但是当我在 Android 应用程序中运行它时,它不起作用,因为类 ImageIO.read(File file)
(Bufferedimage) 未在 Android 中实现。
我读过一些有关使用 Bitmap 类的内容,但我没有找到任何有关它的信息。
您能帮我使用下面找到的这个方法吗?
public static int[][][] getImageRgb(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
int[][][] rgb = new int[height][width][3];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int pixel = image.getRGB(j, i);
rgb[i][j] = getPixelRgb(pixel); }
}
return rgb;
}
其中 getPixelRgb 是一个函数,其目的是:
public static int[] getPixelRgb(int pixel) {
// int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
return new int[]{red, green, blue};
}
我真的不知道如何将这个方法转换为Android。
我期待您的来信。 非常感谢。
最佳答案
你需要的在官方 docs :
int getPixel (int x, int y)
Returns the Color at the specified location.
您可以从 res/drawable 文件夹中的资源创建位图,或者如果您要下载图像,则需要先将其保存到设备存储中。
关于java - 在Android中获取JPG的RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43087321/