Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not sure how can I convert 'Image' from 'ImageReader' to 'yuv frame' #262

Closed
insthync opened this issue Dec 9, 2016 · 5 comments
Closed

Comments

@insthync
Copy link

insthync commented Dec 9, 2016

Hi, Now I'm developing broadcasting app which users can broadcast their screen to another users

I have try to creating new class called 'SrsScreenView' which is based on 'SrsCameraView'

I try to recording the screen by following codes

private void startRecording() {
    Log.d(TAG, "startRecording()");

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int density = metrics.densityDpi;

    if (mHandlerLoop == null)
    {
        mHandlerLoop = new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                mHandler = new Handler();
                Looper.loop();
            }
        };
        mHandlerLoop.start();
    }

    // Start the video input.
    mImageReader = ImageReader.newInstance(mPreviewWidth, mPreviewHeight, PixelFormat.RGBA_8888, 2);

    mVirtualDisplay = mMediaProjection.createVirtualDisplay("Recording Display", mPreviewWidth, mPreviewHeight, density, 0 /* flags */, mImageReader.getSurface(), null /* callback */, mHandler);

    mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler);
}

private class ImageAvailableListener implements ImageReader.OnImageAvailableListener {
    @Override
    public void onImageAvailable(ImageReader reader) {
        Image image = null;
        FileOutputStream fos = null;
        Bitmap bitmap = null;

        ByteArrayOutputStream stream = null;

        try {
            image = mImageReader.acquireLatestImage();
            if (image != null) {
                Image.Plane[] planes = image.getPlanes();
                ByteBuffer buffer = planes[0].getBuffer();
                int pixelStride = planes[0].getPixelStride();
                int rowStride = planes[0].getRowStride();
                int rowPadding = rowStride - pixelStride * mPreviewWidth;

                // create bitmap
                bitmap = Bitmap.createBitmap(mPreviewWidth + rowPadding / pixelStride, mPreviewHeight, Bitmap.Config.ARGB_8888);
                bitmap.copyPixelsFromBuffer(buffer);

                final Bitmap finalBitmap = bitmap;
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (testBitmap != null)
                            testBitmap.setImageBitmap(finalBitmap);
                    }
                });
                // TODO: Have to convert bitmap/image to yuv frame
                //if (mPrevCb != null)
                //    mPrevCb.onGetYuvFrame(mYuvPreviewFrame);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }

            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }

            if (bitmap != null) {
                //bitmap.recycle();
            }

            if (image != null) {
                image.close();
            }
        }
    }
}

I struck at where I placed todo comment, I don't know how can I convert 'Image' or 'Bitmap' to 'yuv frame', I've tested the bitmap, it's work well.

@begeekmyfriend
Copy link
Owner

No no no. See #248

@insthync
Copy link
Author

insthync commented Dec 9, 2016

Does not sure what I have to do. I have following the codes that you share, Now I've change surface input to the surface view. But I don't know where should I calling mPrevCb.onGetYuvFrame(data);

@begeekmyfriend
Copy link
Owner

Use MediaProjection for screen recording which yasea does not support currently

@insthync
Copy link
Author

Okay, I see

@AndroidDeveloperLB
Copy link

The image I got from this has black frames around it. Here's a StackOverflow question I wrote about it:
http://stackoverflow.com/q/43705756/878126

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants