doudao1369 2016-12-28 09:28
浏览 82
已采纳

在Java和PHP中获取RGB有不同的结果

i want to get RGB code for each pixel. As example i took pixel(0,0) (corner left). I load JPEG image and then store in Bitmap object in Java. For experiment i attach the image for example :

enter image description here

I take RGB with PHP and Java. Here the code :

Java (rgbImage is Bitmap object which loaded from JPEG file) :

int width = rgbImage.getWidth();
int height = rgbImage.getHeight();
for (int x = 0; x < rgbImage.getWidth(); x++){
 for (int y = 0; y < rgbImage.getHeight(); y++) {
  int pixel = rgbImage.getPixel(x, y);
    double red = Color.red(pixel);
    double green = Color.green(pixel);
    double blue = Color.blue(pixel);

    if(x == 0 && y == 0){
      System.out.println("red : "+red+" green : "+green+" blue : "+blue);
    }

PHP :

    $rgbImage = imagecreatefromjpeg("$path");
    $width = imagesx($rgbImage);
    $height = imagesy($rgbImage);

    for($x = 0 ; $x < $width ; $x++){
        for($y = 0 ; $y < $height ; $y++){
            $rgb = imagecolorat($rgbImage, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;

            if($x == 0 && $y == 0){
                echo("r : $r , g : $g, b : $b <br/>");
            }


        }
    }

And the result both of them are :

JAVA : I/System.out: red : 91.0 green : 91.0 blue : 101.0

PHP : r : 93 , g : 91, b : 102

The Main Question is :

Why with the same image, two methods above can give different result?

  • 写回答

2条回答 默认 最新

  • doujiao6507 2016-12-28 10:00
    关注

    The JPEG use a specific compression based on the discrete cosine transform (DCT).

    This compression will use a mathematic formula to reduce the size of the information based on area of pixel. This will lead to floating values that will need to be rounded at some point. Of course, the opposite is true, to rebuild a bitmap from a JPEG, some math are needed, based on how this will be implemented will lead in differences because might round some values differently.

    So basicly, you have a painting (bmp file), you want to reduce the detail of it (number of information = size of the file), for that, you can use water to dilute the paint, the painting is still pretty from a distance. But once you have done that, the details that you will see will depends on your brain that will imagine them. Each brain (algorythm to recreate a bitmap) will see a slighty different painting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波