title: bitmap to yuv , video recorded has only green pixels
body: <p>I am trying to convert a bitmap to yuv, and recording this yuv in the ffmpeg frame recorder...
  I am getting the video output with only green pixels, though when i check the properties of this video it shows the set Frame rate and the resolution...</p>
  
  <p>The yuv encoding part is correct, but i feel i am making mistake somewhere else, mostly in returning the yuv bytes to recording part ( getByte(byte [] yuv ) because only there the yuv.length displayed in console is 0,, rest all methods return a big value in console ...</p>
  
  <p>Kindly help...</p>
  
  <pre><code> @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      directory.mkdirs();
  
      addListenerOnButton();
  
      play=(Button)findViewById(R.id.buttonplay);
      stop=(Button)findViewById(R.id.buttonstop);
      record=(Button)findViewById(R.id.buttonstart);
  
      stop.setEnabled(false);
      play.setEnabled(false);
  
  
      record.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startRecording();
              getByte(new byte[]{});
          }
      });
  
      stop.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              stopRecording();
          }
      });
  
  
      play.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) throws IllegalArgumentException, SecurityException, IllegalStateException {
              Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(asmileys)));
              intent.setDataAndType(Uri.parse(String.valueOf(asmileys)), "video/mp4");
              startActivity(intent);
              Toast.makeText(getApplicationContext(), "Playing Video", Toast.LENGTH_LONG).show();
          }
      });
  
   }
  
  ......//......
  
  
  
  public void getByte(byte[] yuv) {
      getNV21(640, 480, bitmap);
      System.out.println(yuv.length + " ");
      if (audioRecord == null || audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
          startTime = System.currentTimeMillis();
          return;
      }
      if (RECORD_LENGTH &gt; 0) {
          int i = imagesIndex++ % images.length;
          yuvimage = images[i];
          timestamps[i] = 1000 * (System.currentTimeMillis() - startTime);
      }
          /* get video data */
      if (yuvimage != null &amp;&amp; recording) {
              ((ByteBuffer) yuvimage.image[0].position(0)).put(yuv);
  
              if (RECORD_LENGTH &lt;= 0) {
                  try {
                      long t = 1000 * (System.currentTimeMillis() - startTime);
                      if (t &gt; recorder.getTimestamp()) {
                          recorder.setTimestamp(t);
                      }
                      recorder.record(yuvimage);
                  } catch (FFmpegFrameRecorder.Exception e) {
  
                      e.printStackTrace();
                  }
              }
          }
  }
  
  public byte [] getNV21(int inputWidth, int inputHeight, Bitmap bitmap) {
  
      int[] argb = new int[inputWidth * inputHeight];
  
      bitmap.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
  
      byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2];
      encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
  
      bitmap.recycle();
      System.out.println(yuv.length + " ");
      return yuv;
  
  }
  
  void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
      final int frameSize = width * height;
  
      int yIndex = 0;
      int uIndex = frameSize;
      int vIndex = frameSize;
      System.out.println(yuv420sp.length + " " + frameSize);
  
      int a, R, G, B, Y, U, V;
      int index = 0;
      for (int j = 0; j &lt; height; j++) {
          for (int i = 0; i &lt; width; i++) {
  
              a = (argb[index] &amp; 0xff000000) &gt;&gt; 24; // a is not used obviously
              R = (argb[index] &amp; 0xff0000) &gt;&gt; 16;
              G = (argb[index] &amp; 0xff00) &gt;&gt; 8;
              B = (argb[index] &amp; 0xff) &gt;&gt; 0;
  
              // well known RGB to YUV algorithm
  
              Y = ((66 * R + 129 * G + 25 * B + 128) &gt;&gt; 8) + 16;
              U = ((-38 * R - 74 * G + 112 * B + 128) &gt;&gt; 8) + 128;
              V = ((112 * R - 94 * G - 18 * B + 128) &gt;&gt; 8) + 128;
  
              // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
              //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other
              //    pixel AND every other scanline.
              yuv420sp[yIndex++] = (byte) ((Y &lt; 0) ? 0 : ((Y &gt; 255) ? 255 : Y));
              if (j % 2 == 0 &amp;&amp; index % 2 == 0) {
                  yuv420sp[uIndex++] = (byte) ((U &lt; 0) ? 0 : ((U &gt; 255) ? 255 : U));
                  yuv420sp[vIndex++] = (byte) ((V &lt; 0) ? 0 : ((V &gt; 255) ? 255 : V));
              }
  
              index++;
          }
      }
  }
  
   .....//.....
  
   public void addListenerOnButton() {
   image = (ImageView) findViewById(R.id.imageView);
   image.setDrawingCacheEnabled(true);
   image.buildDrawingCache();
   bitmap = image.getDrawingCache();
   System.out.println(bitmap.getByteCount() + " " );
  
   button = (Button) findViewById(R.id.btn1);
   button.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View view){
      image.setImageResource(R.drawable.image1);
    }
  });
  
  ......//......
  </code></pre>
  
  <p>EDIT 1:</p>
  
  <p>I made few changes in the above code:</p>
  
  <pre><code> record.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              startRecording();
              getByte();
          }
      });
  .....//....
  
  public void getbyte() {
      byte[] yuv = getNV21(640, 480, bitmap);
  </code></pre>
  
  <p>So now in the console; i get same yuv length in this method as the yuv length from getNV21 method..</p>
  
  <p>But now i am getting half screen Black and Half screen green(black above and green below) pixels in the recorded video...</p>
  
  <p>If i add these lines to onCreate method;</p>
  
  <pre><code> image = (ImageView) findViewById(R.id.imageView);
   image.setDrawingCacheEnabled(true);
   image.buildDrawingCache();
   bitmap = image.getDrawingCache();
  </code></pre>
  
  <p>I do get distorted frames( frames are 1/4th of the image displayed with mix up of colors here and there) in the video....</p>
  
  <p>All i am trying to learn is the image processing and flow of Bytes[] from one method to another; but i am still a noob..; </p>
  
  <p>Kindly help..!</p>