Android Bitmap.getPixels() 返回与 BufferedImage.getRGB() 不同的数据

发布于 2024-12-11 01:07:58 字数 5117 浏览 1 评论 0 原文

我生成了一个测试条形码图像

barcode

http://generator.onbarcode.com/linear.aspx?TYPE=7&DATA=ID%3ao0123456789&UOM=0&X=1&Y=60&LEFT-MARGIN=0&RIGHT-MARGIN=0& ;上边距=0&下边距IN=0&分辨率=0&旋转=0&条形码-宽度=0&条形码-高度=0&显示文本=true&文本-字体=Arial|9|常规&TextMargin=6&格式=png&处理-波浪线=假

我将其保存在本地,并尝试使用一个简单的 java 程序加载它并打印出第一个扫描线,如下所示:

File bmpFile = new File("barcode.bmp");
BufferedImage img;
try {
    img = ImageIO.read(bmpFile);
    int[] pixels = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth());
    int w = img.getWidth();
    for (int i = 0; i< w; i++) {
        System.out.print(pixels[i]+", ");
    }
    System.out.println();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

然后我尝试使用以下代码在模拟器上的 Android 应用程序中加载位图

Bitmap barcode = BitmapFactory.decodeResource(context.getResources(), R.raw.barcode);
Config cfg = globe.getConfig();
int w = barcode.getWidth();

for (int i = 0; i < w; i++) {
    int color = barcode.getPixel(i, 0);
    System.out.print(color+", ");
}
System.out.println();

令人难以置信的是,这两个简单的应用程序必须完全不同扫描线数据。

来自 Java 程序的扫描线数据:

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -16777216, -16777216, - 1, -16777216, -1, -1, -16777216, -1, -1, -1, -1, -16777216, -16777216, -1, -1, -1, -16777216, -1, -1, -1, -16777216, -1, -16777216, -1, -16777216, -16777216, -1, -1, -1 , -16777216, -1, -1, -1, -16777216, -16777216, -16777216, -1, -1, -16777216, -1, -1, -16777216, -16777216, -1, -16777216, -1, -1, -1, -16777216, -16777216 , -16777216, -16777216, -1, -16777216, -1, -16777216, -1, -16777216, -16777216, -16777216, -1, -16777216, -16777216, -16777216, -16777216, -1, -16777216, -16777216, -1, -1, -16777216, -16777216, -1, -16777216, -16777216, -1, -1, -16777216, -16777216, -16777216, -1, -16777216, -16777216, -1, -16777216, -16777216, -16777216, -1, -16777216, -1, -16777216, -16777216, -16777216, -1, -16777216, -16777216, -1, -1, -1, -16777216, -1, -1, -1, -1, -16777216, -1, -16777216, -16777216, -1, -1, -16777216, -16777216 , -1, -16777216, -16777216, -1, -16777216, -16777216, -16777216, -16777216, -1, -16777216, -1, -16777216, -16777216, -1, -1, -1, -16777216, -1, -1 , -1, -16777216, -16777216, -1, -1, -1, -16777216, -16777216, -16777216, -1, -16777216, -1, -16777216, -16777216, -1, -1, -1, -1 , -1, -1, -1, -1, -1, -1,


来自 Android 应用的扫描线数据

-1, -1, -1, -1, -1, -1, -1, -1, - 1、-1、-1、-1、-1、-1、-2170914、-13026247、-16777216、-15197160、-4342083、 -7565684, -15197160, -4342083, -1, -2170914, -13026247, -9736853, -1, -1, -1, -1, -1, -7565684, -16777216, -16777216, -9736853, -1 , -1, -1, -1052689, -13026247, -9736853, -1, -1, -1, -1052689, -13026247, -10855078, -1052689, -13026247, -10855078, -1052689, -13026247, -16777216, -16249848, -4342083, -1, -1, -1, -6512996, -16249848, -4342083, -1, -1, -1, -6512996, -16777216, -16777216, -16777216, -16249848, -5394771, -1, -1052689, -11907766, -10855078, -1, -1, -6512996, -16777216, -16777216, -10855078, -1, -11907766, -10855078, -1, -1, -1, -1, -11907766, -16777216, -16777216, -16777216, -16777216, -16777216, -5394771, -5394771, -16777216, -5394771, -5394771, -16777216, -5394771, -5394771, -16777216, -16777216, -16777216, -16777216, -5394771, -5394771, -16777216, -16777216, -16777216, -16777216, -16777216, -11907766, -1, -10855078, -16777216, -16777216, -6512996, -1, -1, -10855078, -16777216, -16777216, -6512996, -5394771, -16249848, -16777216, -11907766, -1052689, -1, -5394771, -16249848, -16777216, -16777216, -16777216, -6512996, -5394771, -16249848, -16777216, -13026247, -1052689, -10855078, -16777216, -16777216, -16777216, -13026247, -1052689, -10855078, -13026247, -1052689, -10855078, -16777216, -16777216, -16777216, -13026247, -1052689, -9736853, -16777216, -16777216、-7565684、-1、-1、-1、-4342083、-16249848、-7565684、-1、-1、-1、-1、-1、-9736853、-13026247、-2170914、-9736853 ,-16777216, -16777216, -7565684, -1, -1, -9736853, -16777216, -16777216, -7565684, -4342083, -15197160, -16777216, -14078935, -2170914, -9736853, -16777216, -16777216, -16777216, -16777216, -16777216, -7565684, -3223602, -15197160, -7565684, -3223602, -15197160, -16777216, -14078935, -2170914, -1, -1, -1, -8684165, -14078935, -2170914, -1, -1, -1, -8684165, -16777216, -16777216, -8684165, -1 , -1, -1, -3223602, -14078935, -16777216, -16777216, -16777216, -8684165, -3223602, -14078935, -8684165, -3223602, -14078935, -16777216、-15197160、-3223602、-1、-1、-1、-1、-1、-1、-1、-1、-1、-1、-1、-1、-1、-1 ,

我觉得java程序的扫描线数据更合理。在 Android 应用程序中,我尝试将位图放在 res/drawable/res/raw/ 下,但是,返回的是相同的扫描线数据。有人能告诉我这是怎么发生的吗?

I've generated a testing barcode image,

barcode

http://generator.onbarcode.com/linear.aspx?TYPE=7&DATA=ID%3ao0123456789&UOM=0&X=1&Y=60&LEFT-MARGIN=0&RIGHT-MARGIN=0&TOP-MARGIN=0&BOTTOM-MARGIN=0&RESOLUTION=0&ROTATE=0&BARCODE-WIDTH=0&BARCODE-HEIGHT=0&SHOW-TEXT=true&TEXT-FONT=Arial|9|Regular&TextMargin=6&FORMAT=png&PROCESS-TILDE=false

I saved it locally and tried to load it with a simple java program and print out the first scanline as following:

File bmpFile = new File("barcode.bmp");
BufferedImage img;
try {
    img = ImageIO.read(bmpFile);
    int[] pixels = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth());
    int w = img.getWidth();
    for (int i = 0; i< w; i++) {
        System.out.print(pixels[i]+", ");
    }
    System.out.println();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

And then I tried to load the bitmap in an Android app on emulator with following code

Bitmap barcode = BitmapFactory.decodeResource(context.getResources(), R.raw.barcode);
Config cfg = globe.getConfig();
int w = barcode.getWidth();

for (int i = 0; i < w; i++) {
    int color = barcode.getPixel(i, 0);
    System.out.print(color+", ");
}
System.out.println();

Unbelievably, those two simple application have to totally different scanline data.

scanline data from Java program:

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -16777216, -16777216, -1, -16777216, -1, -1, -16777216, -1, -1, -1, -1, -16777216, -16777216, -1, -1, -1, -16777216, -1, -1, -1, -16777216, -1, -16777216, -1, -16777216, -16777216, -1, -1, -1, -16777216, -1, -1, -1, -16777216, -16777216, -16777216, -1, -1, -16777216, -1, -1, -16777216, -16777216, -1, -16777216, -1, -1, -1, -16777216, -16777216, -16777216, -16777216, -1, -16777216, -1, -16777216, -1, -16777216, -16777216, -16777216, -1, -16777216, -16777216, -16777216, -16777216, -1, -16777216, -16777216, -1, -1, -16777216, -16777216, -1, -16777216, -16777216, -1, -1, -16777216, -16777216, -16777216, -1, -16777216, -16777216, -1, -16777216, -16777216, -16777216, -1, -16777216, -1, -16777216, -16777216, -16777216, -1, -16777216, -16777216, -1, -1, -1, -16777216, -1, -1, -1, -1, -16777216, -1, -16777216, -16777216, -1, -1, -16777216, -16777216, -1, -16777216, -16777216, -1, -16777216, -16777216, -16777216, -16777216, -1, -16777216, -1, -16777216, -16777216, -1, -1, -1, -16777216, -1, -1, -1, -16777216, -16777216, -1, -1, -1, -16777216, -16777216, -16777216, -1, -16777216, -1, -16777216, -16777216, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

Scanline data from Android app:

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2170914, -13026247, -16777216, -15197160, -4342083, -7565684, -15197160, -4342083, -1, -2170914, -13026247, -9736853, -1, -1, -1, -1, -1, -7565684, -16777216, -16777216, -9736853, -1, -1, -1, -1052689, -13026247, -9736853, -1, -1, -1, -1052689, -13026247, -10855078, -1052689, -13026247, -10855078, -1052689, -13026247, -16777216, -16249848, -4342083, -1, -1, -1, -6512996, -16249848, -4342083, -1, -1, -1, -6512996, -16777216, -16777216, -16777216, -16249848, -5394771, -1, -1052689, -11907766, -10855078, -1, -1, -6512996, -16777216, -16777216, -10855078, -1, -11907766, -10855078, -1, -1, -1, -1, -11907766, -16777216, -16777216, -16777216, -16777216, -16777216, -5394771, -5394771, -16777216, -5394771, -5394771, -16777216, -5394771, -5394771, -16777216, -16777216, -16777216, -16777216, -5394771, -5394771, -16777216, -16777216, -16777216, -16777216, -16777216, -11907766, -1, -10855078, -16777216, -16777216, -6512996, -1, -1, -10855078, -16777216, -16777216, -6512996, -5394771, -16249848, -16777216, -11907766, -1052689, -1, -5394771, -16249848, -16777216, -16777216, -16777216, -6512996, -5394771, -16249848, -16777216, -13026247, -1052689, -10855078, -16777216, -16777216, -16777216, -13026247, -1052689, -10855078, -13026247, -1052689, -10855078, -16777216, -16777216, -16777216, -13026247, -1052689, -9736853, -16777216, -16777216, -7565684, -1, -1, -1, -4342083, -16249848, -7565684, -1, -1, -1, -1, -1, -9736853, -13026247, -2170914, -9736853, -16777216, -16777216, -7565684, -1, -1, -9736853, -16777216, -16777216, -7565684, -4342083, -15197160, -16777216, -14078935, -2170914, -9736853, -16777216, -16777216, -16777216, -16777216, -16777216, -7565684, -3223602, -15197160, -7565684, -3223602, -15197160, -16777216, -14078935, -2170914, -1, -1, -1, -8684165, -14078935, -2170914, -1, -1, -1, -8684165, -16777216, -16777216, -8684165, -1, -1, -1, -3223602, -14078935, -16777216, -16777216, -16777216, -8684165, -3223602, -14078935, -8684165, -3223602, -14078935, -16777216, -15197160, -3223602, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

It seems to me that the scanline data from java program is more reasonable. In Android app, I've tried to put the bitmap under both res/drawable/ and res/raw/, however, the same scanline data is returned. Can someone enlighten me how this could happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

跨年 2024-12-18 01:07:58

您是否从drawables文件夹加载位图?我遇到一个问题,在某些设备上,在这种情况下位图会略有变化。我通过将位图资源放入原始文件夹中解决了这个问题

Are you loading bitmaps from the drawables folder? I had a problem where on some devices, the bitmaps would be changed slightly in this case. I solved it by putting the bitmap resource in the raw folder

生活了然无味 2024-12-18 01:07:58

要更改图像的类型,您可以使用以下代码。代码已经过测试。

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mBitmap = BitmapFactory.decodeFile(imagePath, opt); 

但这没有帮助。我在 JDK 和 Android 上对不同的数字(来自 getPixel 和 getRGB)也有类似的问题。

但是,我的代码是在客户端(android)和服务器(JDK)上计算直方图。然后必须对它们进行比较。

问题解决了吗?

UPD。当时的问题是JPG格式。

To change the type of the image, you can use the below code. The code is tested.

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mBitmap = BitmapFactory.decodeFile(imagePath, opt); 

But that does not help. I have a similar problem with different numbers (from getPixel and getRGB) on JDK and Android.

However, my code is to compute histograms on the client (android) and the server (JDK). Then they must be compared.

Is the Problem solved?

UPD. The problem at that time was the JPG format.

抠脚大汉 2024-12-18 01:07:58

看起来您正在加载 565 中的位图。将 BitmapFactory.Options 传递给decode() 并强制解码 ARGB_8888。

Looks like you are loading the bitmap in 565. Pass a BitmapFactory.Options to decode() and force decoding in ARGB_8888.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文