admin管理员组

文章数量:1315318

I need to convert inputImage to file and upload it. on ios device everything going well, but on android I have blue tint on every my image. my code is like that :

img.Image decodeYUV420SP(InputImage image) {
  final width = image.metadata!.size.width.toInt();
  final height = image.metadata!.size.height.toInt();
  final yuv420sp = image.bytes!;
  final frameSize = width * height;

  final outImg = img.Image(width: width, height: height, numChannels: 3);

  for (int j = 0, yp = 0; j < height; j++) {
    int uvp = frameSize + (j >> 1) * width;
    int u = 0, v = 0;

    for (int i = 0; i < width; i++, yp++) {
      int y = (0xff & yuv420sp[yp]) - 16;
      if (y < 0) y = 0;

      if ((i & 1) == 0) {
        v = (0xff & yuv420sp[uvp++]) - 128;
        u = (0xff & yuv420sp[uvp++]) - 128;
      }

     
      int r = (1.164 * y + 1.596 * v).round();
      int g = (1.164 * y - 0.392 * u - 0.813 * v).round();
      int b = (1.164 * y + 2.017 * u).round();

      
      r = r.clamp(0, 255);
      g = g.clamp(0, 255);
      b = b.clamp(0, 255);

      
      outImg.setPixelRgb(i, j, r, g, b);
    }
  }

  }
}

I need to convert inputImage to file and upload it. on ios device everything going well, but on android I have blue tint on every my image. my code is like that :

img.Image decodeYUV420SP(InputImage image) {
  final width = image.metadata!.size.width.toInt();
  final height = image.metadata!.size.height.toInt();
  final yuv420sp = image.bytes!;
  final frameSize = width * height;

  final outImg = img.Image(width: width, height: height, numChannels: 3);

  for (int j = 0, yp = 0; j < height; j++) {
    int uvp = frameSize + (j >> 1) * width;
    int u = 0, v = 0;

    for (int i = 0; i < width; i++, yp++) {
      int y = (0xff & yuv420sp[yp]) - 16;
      if (y < 0) y = 0;

      if ((i & 1) == 0) {
        v = (0xff & yuv420sp[uvp++]) - 128;
        u = (0xff & yuv420sp[uvp++]) - 128;
      }

     
      int r = (1.164 * y + 1.596 * v).round();
      int g = (1.164 * y - 0.392 * u - 0.813 * v).round();
      int b = (1.164 * y + 2.017 * u).round();

      
      r = r.clamp(0, 255);
      g = g.clamp(0, 255);
      b = b.clamp(0, 255);

      
      outImg.setPixelRgb(i, j, r, g, b);
    }
  }

  }
}
Share Improve this question asked Jan 30 at 9:21 Lala NaibovaLala Naibova 4431 gold badge4 silver badges23 bronze badges 2
  • Please edit your question to provide a minimal reproducible example. Make sure everything is there that is needed to actually execute your code while omitting everything that is not essential. – tyg Commented Jan 30 at 10:36
  • 1 You might want to look carefully at the coefficients used in your transform to RGB space they don't match the ones that I would expect or those at the Wiki Y'UV page – Martin Brown Commented Jan 30 at 12:27
Add a comment  | 

1 Answer 1

Reset to default 0

I have changed jpeg to png and problem solved.

 img.Image? decodedImage = decodeYUV420SP(inputImage);
 List<int> encodedBytes = img.encodePng(decodedImage);

previous was like that => img.encodeJpg(decodedImage, quality: 100);

本文标签: androidHow to avoid blue tint when converting to imageStack Overflow