title: Display the YUV value from live camera preview Android
body: <p>This question has been asked many times already but i cannot seem to find what i want exactly.
  I am trying to create a camera app where I want to display the YUV or RGB value into the log when I point my camera to some color. The values must me 0...255 range for RGB or correspondent YUV color format. I can manage the conversion between them as there are many such examples on stack overflow. However, i cannot store the values into 3 separate variables and display them in the log.</p>
  
  <p>so far i have managed to get</p>
  
  <pre><code>package com.example.virus.bpreader;
  
  public class CaptureVideo extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {
  
  private SurfaceHolder mHolder;
  private Camera mCamera;
  private int[] pixels;
  
  public CaptureVideo(Context context, Camera cameraManager) {
      super(context);
  
      mCamera = cameraManager;
      mCamera.setDisplayOrientation(90);
  
      //get holder and set the class as callback
      mHolder = getHolder();
      mHolder.addCallback(this);
      mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
  }
  
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
  
      //when the surface is created, let the camera start the preview
      try {
          mCamera.setPreviewDisplay(holder);
          mCamera.startPreview();
          mCamera.cancelAutoFocus();
  
          Camera.Parameters params = mCamera.getParameters();
          //get fps
          params.getSupportedPreviewFpsRange();
          //get resolution
          params.getSupportedPreviewSizes();
          //stop auto exposure
          params.setAutoExposureLock(false);
  
          // Check what resolutions are supported by your camera
          List&lt;Camera.Size&gt; sizes = params.getSupportedPictureSizes();
          // Iterate through all available resolutions and choose one
          for (Camera.Size size : sizes) {
              Log.i("Resolution", "Available resolution: " + size.width + " " + size.height);
          }
          //set resolution at 320*240
          params.setPreviewSize(320,240);
  
          //set frame rate at 10 fps
          List&lt;int[]&gt; frameRates = params.getSupportedPreviewFpsRange();
          int last = frameRates.size() - 1;
          params.setPreviewFpsRange(10000, 10000);
          //set Image Format
          //params.setPreviewFormat(ImageFormat.NV21);
          mCamera.setParameters(params);
  
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  
  
  }
  
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
      //need to stop the preview and restart on orientation change
      if (mHolder.getSurface() == null) {
          return;
      }
      //stop preview
      mCamera.stopPreview();
  
      //start again
      try {
          mCamera.setPreviewDisplay(holder);
          mCamera.startPreview();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
  
  }
  
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
      //stop and release
      mCamera.startPreview();
      mCamera.release();
  }
  
  @Override
  public void onPreviewFrame(byte[] data, Camera camera) {
  
      int frameHeight = camera.getParameters().getPreviewSize().height;
      int frameWidth = camera.getParameters().getPreviewSize().width;
      // number of pixels//transforms NV21 pixel data into RGB pixels
      int rgb[] = new int[frameWidth * frameHeight];
      // convertion
      int[] myPixels = decodeYUV420SP(rgb, data, frameWidth, frameHeight);
  
      Log.d("myPixel", String.valueOf(myPixels.length));
  
  }
  
  //yuv decode
  int[] decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width, int height) {
  
      final int frameSize = width * height;
      int r, g, b, y1192, y, i, uvp, u, v;
      for (int j = 0, yp = 0; j &lt; height; j++) {
          uvp = frameSize + (j &gt;&gt; 1) * width;
          u = 0;
          v = 0;
          for (i = 0; i &lt; width; i++, yp++) {
              y = (0xff &amp; ((int) yuv420sp[yp])) - 16;
              if (y &lt; 0)
                  y = 0;
              if ((i &amp; 1) == 0) {
                  // above answer is wrong at the following lines. just swap ***u*** and ***v***
                  u = (0xff &amp; yuv420sp[uvp++]) - 128;
                  v = (0xff &amp; yuv420sp[uvp++]) - 128;
              }
  
              y1192 = 1192 * y;
              r = (y1192 + 1634 * v);
              g = (y1192 - 833 * v - 400 * u);
              b = (y1192 + 2066 * u);
  
              r = Math.max(0, Math.min(r, 262143));
              g = Math.max(0, Math.min(g, 262143));
              b = Math.max(0, Math.min(b, 262143));
  
              // combine RGB
              rgb[yp] = 0xff000000 | ((r &lt;&lt; 6) &amp; 0xff0000) | ((g &gt;&gt; 2) &amp; 0xff00) | ((b &gt;&gt; 10) | 0xff);
          }
      }
  return rgb;
  }
  }
  </code></pre>
  
  <p>Here the decode method should give a hex format of RGB color space (quite sure its all right as most of the answers are the same). The problem i am facing is when i am not quite sure how to call it inside the OnPreviewFrame method so that it displays the RGB values separately into the log. </p>
  
  <p>N.B like i said i have seen lot of similar questions but could not find a solution to it. I do not want to store the file (image/video) as i only need the RGB/YUV values from the live camera preview when i point the camera to some color.</p>
  
  <p>I need the rgb or yuv values because i want to plot a graph out of it against time. </p>
  
  <p>Any help will be much appreciated.   </p>