doudao1369 2016-12-28 01: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 02: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部