How to correctly receive camera images from onImageAvailable?
<p>I am trying to achieve video streaming from one android device to another. In order to do this I want to obtain frames from camera, send them through sockets and show on ImageView on another phone. The problem is that I have to use ImageReader with specific format - YUV_420_888. When I try to create bitmap based on bytes from this image I can only get ALPHA_8(other formats cause Exceptions saying that buffer is not big enough for pixels), which is black and white + wrongly oriented. I would like to know if there is a way to obtain correct version directly or somehow convert it so I can make a proper bitmap ready to show on ImageView. Here's some code:</p>

<pre><code>public class MainActivity extends AppCompatActivity
{
  private ImageReader mImageReader;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.imageView = (ImageView) findViewById(R.id.image);

        this.handler = new Handler(){
            @Override
            public void handleMessage(Message msg)
            {
                if(msg.what == Worker.PROCESSED_IMAGE)
                {
                    imageView.setImageBitmap((Bitmap) msg.obj);
                }
            }
        };
    }

    private void openCamera()
    {
        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

        try
        {
            String cameraId = manager.getCameraIdList()[0];
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
            StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0];
            mImageReader = ImageReader.newInstance(mPreviewSize.getWidth(), mPreviewSize.getHeight(), ImageFormat.YUV_420_888, 50);
            mImageReader.setOnImageAvailableListener(new Worker(this, this.handler, mPreviewSize.getWidth(), mPreviewSize.getHeight()), new Handler());

            manager.openCamera(cameraId, mStateCallback, null);
        }
        catch(CameraAccessException e)
        {
            e.printStackTrace();
        }

    }

}


public class Worker implements ImageReader.OnImageAvailableListener
{
    @Override
    public void onImageAvailable(ImageReader imageReader)
    {
        Image image = imageReader.acquireLatestImage();

        Image.Plane plane = image.getPlanes()[0];
        ByteBuffer buffer = plane.getBuffer();
        Bitmap bm = Bitmap.createBitmap(width, heigth, Bitmap.Config.ALPHA_8);
        bm.copyPixelsFromBuffer(buffer);
        image.close();
        handler.obtainMessage(PROCESSED_IMAGE, bm).sendToTarget();

}
</code></pre>