Rectangle 27 7

I'm working on exactly the same issue, but haven't got quite as far as you have.

Have you considered drawing the pixels directly to the canvas without encoding them to JPEG first? Inside the OpenCV kit http://sourceforge.net/projects/opencvlibrary/files/opencv-android/2.3.1/OpenCV-2.3.1-android-bin.tar.bz2/download (which doesn't actually use opencv; don't worry), there's a project called tutorial-0-androidcamera that demonstrates converting the YUV pixels to RGB and then writing them directly to a bitmap.

public void onPreviewFrame(byte[] data, Camera camera, int width, int height) {
    int frameSize = width*height;
    int[] rgba = new int[frameSize+1];

    // Convert YUV to RGB
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++) {
            int y = (0xff & ((int) data[i * width + j]));
            int u = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1) + 0]));
            int v = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1) + 1]));
            y = y < 16 ? 16 : y;

            int r = Math.round(1.164f * (y - 16) + 1.596f * (v - 128));
            int g = Math.round(1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128));
            int b = Math.round(1.164f * (y - 16) + 2.018f * (u - 128));

            r = r < 0 ? 0 : (r > 255 ? 255 : r);
            g = g < 0 ? 0 : (g > 255 ? 255 : g);
            b = b < 0 ? 0 : (b > 255 ? 255 : b);

            rgba[i * width + j] = 0xff000000 + (b << 16) + (g << 8) + r;
        }

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmp.setPixels(rgba, 0/* offset */, width /* stride */, 0, 0, width, height);
    Canvas canvas = mHolder.lockCanvas();
    if (canvas != null) {
        canvas.drawBitmap(bmp, (canvas.getWidth() - width) / 2, (canvas.getHeight() - height) / 2, null);
        mHolder.unlockCanvasAndPost(canvas);
    } else {
        Log.w(TAG, "Canvas is null!");
    }
    bmp.recycle();
}

Of course you'd have to adapt it to meet your needs (ex. not allocating rgba each frame), but it might be a start. I'd love to see if it works for you or not -- i'm still fighting problems orthogonal to yours at the moment.

I will try this tomorrow, but I'm quite sure that there will be no performance improvement, because this works more or less the same as the version with YuvImage and BitmapFactory classes. But I'll give it a try and we'll see. Thanks.

So, as I expected, it has the worst performance from all the cases, even when I tried to optimize it a bit...

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 7

I'm working on exactly the same issue, but haven't got quite as far as you have.

Have you considered drawing the pixels directly to the canvas without encoding them to JPEG first? Inside the OpenCV kit http://sourceforge.net/projects/opencvlibrary/files/opencv-android/2.3.1/OpenCV-2.3.1-android-bin.tar.bz2/download (which doesn't actually use opencv; don't worry), there's a project called tutorial-0-androidcamera that demonstrates converting the YUV pixels to RGB and then writing them directly to a bitmap.

public void onPreviewFrame(byte[] data, Camera camera, int width, int height) {
    int frameSize = width*height;
    int[] rgba = new int[frameSize+1];

    // Convert YUV to RGB
    for (int i = 0; i < height; i++)
        for (int j = 0; j < width; j++) {
            int y = (0xff & ((int) data[i * width + j]));
            int u = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1) + 0]));
            int v = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1) + 1]));
            y = y < 16 ? 16 : y;

            int r = Math.round(1.164f * (y - 16) + 1.596f * (v - 128));
            int g = Math.round(1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128));
            int b = Math.round(1.164f * (y - 16) + 2.018f * (u - 128));

            r = r < 0 ? 0 : (r > 255 ? 255 : r);
            g = g < 0 ? 0 : (g > 255 ? 255 : g);
            b = b < 0 ? 0 : (b > 255 ? 255 : b);

            rgba[i * width + j] = 0xff000000 + (b << 16) + (g << 8) + r;
        }

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmp.setPixels(rgba, 0/* offset */, width /* stride */, 0, 0, width, height);
    Canvas canvas = mHolder.lockCanvas();
    if (canvas != null) {
        canvas.drawBitmap(bmp, (canvas.getWidth() - width) / 2, (canvas.getHeight() - height) / 2, null);
        mHolder.unlockCanvasAndPost(canvas);
    } else {
        Log.w(TAG, "Canvas is null!");
    }
    bmp.recycle();
}

Of course you'd have to adapt it to meet your needs (ex. not allocating rgba each frame), but it might be a start. I'd love to see if it works for you or not -- i'm still fighting problems orthogonal to yours at the moment.

I will try this tomorrow, but I'm quite sure that there will be no performance improvement, because this works more or less the same as the version with YuvImage and BitmapFactory classes. But I'll give it a try and we'll see. Thanks.

So, as I expected, it has the worst performance from all the cases, even when I tried to optimize it a bit...

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 4

I think Michael's on the right track. First you can try this method to convert from RGB to Grayscale. Clearly it's doing almost the same thing as his,but a little more succinctly for what you want.

//YUV Space to Greyscale
static public void YUVtoGrayScale(int[] rgb, byte[] yuv420sp, int width, int height){
    final int frameSize = width * height;
    for (int pix = 0; pix < frameSize; pix++){
        int pixVal = (0xff & ((int) yuv420sp[pix])) - 16;
        if (pixVal < 0) pixVal = 0;
        if (pixVal > 255) pixVal = 255;
        rgb[pix] = 0xff000000 | (pixVal << 16) | (pixVal << 8) | pixVal;
    }
}

Second, don't create a ton of work for the garbage collector. Your bitmaps and arrays are going to be a fixed size. Create them once, not in onFramePreview.

Doing that you'll end up with something that looks like this:

public PreviewCallback callback = new PreviewCallback() {
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        if ( (mSelectView == null) || !inPreview )
            return;
        if (mSelectView.mBitmap == null)
        {
            //initialize SelectView bitmaps, arrays, etc
            //mSelectView.mBitmap = Bitmap.createBitmap(mSelectView.mImageWidth, mSelectView.mImageHeight, Bitmap.Config.RGB_565);
           //etc

        }
        //Pass Image Data to SelectView
        System.arraycopy(data, 0, mSelectView.mYUVData, 0, data.length);
        mSelectView.invalidate();
    }
};
class SelectView extends View {
Bitmap mBitmap;
Bitmap croppedView;
byte[] mYUVData;
int[] mRGBData;
int mImageHeight;
int mImageWidth;

public SelectView(Context context){
    super(context);
    mBitmap = null;
    croppedView = null;
}

@Override
protected void onDraw(Canvas canvas){
    if (mBitmap != null)
    {
        int canvasWidth = canvas.getWidth();
        int canvasHeight = canvas.getHeight();
        // Convert from YUV to Greyscale
        YUVtoGrayScale(mRGBData, mYUVData, mImageWidth, mImageHeight);
            mBitmap.setPixels(mRGBData, 0, mImageWidth, 0, 0, mImageWidth, mImageHeight);
            Rect crop = new Rect(180, 220, 290, 400);
        Rect dst = new Rect(0, 0, canvasWidth, (int)(canvasHeight/2));
        canvas.drawBitmap(mBitmap, crop, dst, null);
    }
    super.onDraw(canvas);
}

This example shows a cropped and distorted selection of the camera preview in real time, but you get the idea. It runs at high FPS on a Nexus S in greyscale and should work for your needs as well.

Second, don't create a ton of work for the garbage collector.
onDraw

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 4

I think Michael's on the right track. First you can try this method to convert from RGB to Grayscale. Clearly it's doing almost the same thing as his,but a little more succinctly for what you want.

//YUV Space to Greyscale
static public void YUVtoGrayScale(int[] rgb, byte[] yuv420sp, int width, int height){
    final int frameSize = width * height;
    for (int pix = 0; pix < frameSize; pix++){
        int pixVal = (0xff & ((int) yuv420sp[pix])) - 16;
        if (pixVal < 0) pixVal = 0;
        if (pixVal > 255) pixVal = 255;
        rgb[pix] = 0xff000000 | (pixVal << 16) | (pixVal << 8) | pixVal;
    }
}

Second, don't create a ton of work for the garbage collector. Your bitmaps and arrays are going to be a fixed size. Create them once, not in onFramePreview.

Doing that you'll end up with something that looks like this:

public PreviewCallback callback = new PreviewCallback() {
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        if ( (mSelectView == null) || !inPreview )
            return;
        if (mSelectView.mBitmap == null)
        {
            //initialize SelectView bitmaps, arrays, etc
            //mSelectView.mBitmap = Bitmap.createBitmap(mSelectView.mImageWidth, mSelectView.mImageHeight, Bitmap.Config.RGB_565);
           //etc

        }
        //Pass Image Data to SelectView
        System.arraycopy(data, 0, mSelectView.mYUVData, 0, data.length);
        mSelectView.invalidate();
    }
};
class SelectView extends View {
Bitmap mBitmap;
Bitmap croppedView;
byte[] mYUVData;
int[] mRGBData;
int mImageHeight;
int mImageWidth;

public SelectView(Context context){
    super(context);
    mBitmap = null;
    croppedView = null;
}

@Override
protected void onDraw(Canvas canvas){
    if (mBitmap != null)
    {
        int canvasWidth = canvas.getWidth();
        int canvasHeight = canvas.getHeight();
        // Convert from YUV to Greyscale
        YUVtoGrayScale(mRGBData, mYUVData, mImageWidth, mImageHeight);
            mBitmap.setPixels(mRGBData, 0, mImageWidth, 0, 0, mImageWidth, mImageHeight);
            Rect crop = new Rect(180, 220, 290, 400);
        Rect dst = new Rect(0, 0, canvasWidth, (int)(canvasHeight/2));
        canvas.drawBitmap(mBitmap, crop, dst, null);
    }
    super.onDraw(canvas);
}

This example shows a cropped and distorted selection of the camera preview in real time, but you get the idea. It runs at high FPS on a Nexus S in greyscale and should work for your needs as well.

Second, don't create a ton of work for the garbage collector.
onDraw

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 1

Is this not what you want? Just use a SurfaceView in your layout, then somewhere in your init like onResume():

SurfaceView surfaceView = ...
SurfaceHolder holder = surfaceView.getHolder();
...
Camera camera = ...;
camera.setPreviewDisplay(holder);

It just sends the frames straight to the view as fast as they arrive.

If you want grayscale, modify the camera parameters with setColorEffect("mono").

Thanks, but I really need to use callback and display images manually. I don't need only grayscale image, I'm using some more filters, that I didn't mention in my question...

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 1

Camera.Parameters parameters = mCamera.getParameters();
parameters.setColorEffect(Parameters.EFFECT_AQUA);

I figured out that this effects do DIFFERENTLY depending on the device. For instance, on my phone (galaxy s II) it looks kinda like a comic effect as in contrast to the galaxy s 1 it is 'just' a blue shade.

It's pro: It's working as live-preview.

I looked around some other camera apps and they obviously also faced this problem. So what did they do? They are capturing the default camera image, applying a filter to the bitmap data, and show this image in a simple ImageView. It's for sure not that cool as in live preview, but you won't ever face performance problems.

Thanks, I know about these camera effects, but I wanted to do some contrast adjustement, color substitution etc. But you're right, it seems that there is really not a good working solution to live preview, so I'll have to do something like you described... :/

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 1

Camera.Parameters parameters = mCamera.getParameters();
parameters.setColorEffect(Parameters.EFFECT_AQUA);

I figured out that this effects do DIFFERENTLY depending on the device. For instance, on my phone (galaxy s II) it looks kinda like a comic effect as in contrast to the galaxy s 1 it is 'just' a blue shade.

It's pro: It's working as live-preview.

I looked around some other camera apps and they obviously also faced this problem. So what did they do? They are capturing the default camera image, applying a filter to the bitmap data, and show this image in a simple ImageView. It's for sure not that cool as in live preview, but you won't ever face performance problems.

Thanks, I know about these camera effects, but I wanted to do some contrast adjustement, color substitution etc. But you're right, it seems that there is really not a good working solution to live preview, so I'll have to do something like you described... :/

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 1

Is this not what you want? Just use a SurfaceView in your layout, then somewhere in your init like onResume():

SurfaceView surfaceView = ...
SurfaceHolder holder = surfaceView.getHolder();
...
Camera camera = ...;
camera.setPreviewDisplay(holder);

It just sends the frames straight to the view as fast as they arrive.

If you want grayscale, modify the camera parameters with setColorEffect("mono").

Thanks, but I really need to use callback and display images manually. I don't need only grayscale image, I'm using some more filters, that I didn't mention in my question...

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 2

Usually bluetooth is very slow to send image frames, but you don't need to sends each and every frames. One problem you will face is the frame collision. Bluetooth socket sends your data(frames) as byte array. So while receiving the data from other end most of the times the bytes of previous frame and current frame will collide. So you need to make sure that is the first frame is received by the remote mobile and processed successfully. The frames created while the remote mobile is processing should not be stores in buffer to send later, Rather they should be ignored(Should not send). Some frames may miss, but you will work as live feed. Now the android mobile will create big frames of preview according to the camera resolution. So you need to send small size of frame only, on the remaining space of the display you can show camera controls.

I have posted an example project in my Github repo. Bluetooth Camera

java - How to show camera preview on other device over Bluetooth? - St...

java android bitmap bluetooth camera
Rectangle 27 3

If you don't need to run on Android releases before 3.0 (Honeycomb), you can use the SurfaceTexture output option, and OpenGL ES for rendering multiple previews.

Setting up a working OpenGL view is a bit involved, so you'll want to find a guide for that. Once you have that working, create a SurfaceTexture object with a OpenGL texture ID you want to use for the preview texture, pass that into the camera with setPreviewTexture instead of using setPreviewDisplay, and then in your OpenGL thread, call updateTexImage to update the texture to show the latest frame from the camera (best to do so in response to a onFrameAvailable callback for best performance).

Then you need OpenGL code to render two rectangles for your two copies of preview, and a shader to draw the preview texture on both.

This is a decent bit of setup, but in the end, you'll have a very flexible system - then you can animate the preview in various fun ways, etc.

I am currently using onPreviewFrame to also send some the data to send the data to some third party native code. Is it possible to use your suggested method and still receive the data in onPreviewFrame?

java - How to show two identical camera previews with the Android came...

java android android-camera surfaceview
Rectangle 27 2

Check out the face tracker sample code, which is similar. It also handles getting camera permissions and activity pause/resume:

Note that the "prominentFaceOnly" option will only detect faces that appear relatively large in the camera preview (i.e., face is around 35% or more of the width of the preview image).

How to use camera resource with FaceDetector in android? - Stack Overf...

android camera face-detection
Rectangle 27 0

I think Michael's on the right track. First you can try this method to convert from RGB to Grayscale. Clearly it's doing almost the same thing as his,but a little more succinctly for what you want.

//YUV Space to Greyscale
static public void YUVtoGrayScale(int[] rgb, byte[] yuv420sp, int width, int height){
    final int frameSize = width * height;
    for (int pix = 0; pix < frameSize; pix++){
        int pixVal = (0xff & ((int) yuv420sp[pix])) - 16;
        if (pixVal < 0) pixVal = 0;
        if (pixVal > 255) pixVal = 255;
        rgb[pix] = 0xff000000 | (pixVal << 16) | (pixVal << 8) | pixVal;
    }
}

Second, don't create a ton of work for the garbage collector. Your bitmaps and arrays are going to be a fixed size. Create them once, not in onFramePreview.

Doing that you'll end up with something that looks like this:

public PreviewCallback callback = new PreviewCallback() {
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        if ( (mSelectView == null) || !inPreview )
            return;
        if (mSelectView.mBitmap == null)
        {
            //initialize SelectView bitmaps, arrays, etc
            //mSelectView.mBitmap = Bitmap.createBitmap(mSelectView.mImageWidth, mSelectView.mImageHeight, Bitmap.Config.RGB_565);
           //etc

        }
        //Pass Image Data to SelectView
        System.arraycopy(data, 0, mSelectView.mYUVData, 0, data.length);
        mSelectView.invalidate();
    }
};
class SelectView extends View {
Bitmap mBitmap;
Bitmap croppedView;
byte[] mYUVData;
int[] mRGBData;
int mImageHeight;
int mImageWidth;

public SelectView(Context context){
    super(context);
    mBitmap = null;
    croppedView = null;
}

@Override
protected void onDraw(Canvas canvas){
    if (mBitmap != null)
    {
        int canvasWidth = canvas.getWidth();
        int canvasHeight = canvas.getHeight();
        // Convert from YUV to Greyscale
        YUVtoGrayScale(mRGBData, mYUVData, mImageWidth, mImageHeight);
            mBitmap.setPixels(mRGBData, 0, mImageWidth, 0, 0, mImageWidth, mImageHeight);
            Rect crop = new Rect(180, 220, 290, 400);
        Rect dst = new Rect(0, 0, canvasWidth, (int)(canvasHeight/2));
        canvas.drawBitmap(mBitmap, crop, dst, null);
    }
    super.onDraw(canvas);
}

This example shows a cropped and distorted selection of the camera preview in real time, but you get the idea. It runs at high FPS on a Nexus S in greyscale and should work for your needs as well.

Second, don't create a ton of work for the garbage collector.
onDraw

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 0

Essentially, you want to make the camera preview the first element in the first Layout object in the XML file.

You can use any of the Layout types based on how you would like to layout the other controls. I used a RelativeLayout as the base for my layout here. The FrameLayout with an id of 'camera_preview' is used as a container for the camera preview class.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context=".YourMainActivity" >

    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <!-- ... other display controls ... -->

</RelativeLayout>

Then, in YourMainActivity's onCreate() create a new instance of your camera preview class and add it to the camera_preview container in your layout:

private static YourCameraPreviewClass mPreview;

@Override
protected void onCreate(Bundle savedInstanceState) {

    // ... get camera instance ...

    mPreview = new YourCameraPreviewClass( this, cameraInstance );  
    FrameLayout previewFrameLayout = (FrameLayout) findViewById( R.id.camera_preview );
    previewFrameLayout.addView( mPreview );

    Log.d( LOGTAG, "Camera preview background set!  (:" );

}

Java android - Ho do I set the background of an activity to camera pre...

android background camera
Rectangle 27 0

I believe I read in a blog that the grayscale data is in the first x*y bytes. Yuv should represent luminance, so the data is there, although it isn't a perfect grayscale. Its great for relative brightness, but not grayscale, as each color isn't as bright as each other in rgb. Green is usually given a stronger weight in luminosity conversions. Hope this helps!

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 0

I cannot see where getOptimalPreviewSize() is called; also, there is no real reason to set preview size to match the screen; usually, the default camera preview is chosen by the manufacturer to look good on the screen (which involves scaling).

With all that, the code you published could be significantly simplified. If I understand correctly, you have fixed screenOrientation in your activity, and want to fill all the screen with camera preview, preserving aspect ratio to match the desired picture size.

This simple example, shows how to set width and height for the preview surface view. If this is not enough for you, feel free to ask for more details.

just had a facepalm moment I was not changing mUpdateSet to the correct value before calling the updateCameraParameters(int updateSet) method.The preview is bigger but still not filling the screen entirely.Can we use the fix you describe above on the parent of the PreviewFrameLayout which is a RelativeLayout???

the orientation has not been fixed either

Yes, same solution works for RelativeLayout, if you really need it. But in your case, FrameLayout without even LinearLayout on top of it is more than enough.

Do we need to invert width and height in case it is landscape?

edited with different code for portrait and landscape,will this work for all cases?

java - Camera Preview does not appear full screen(displayed in a box) ...

java android android-camera surfaceview aspect-ratio
Rectangle 27 0

You will never get ratio that 'fits perfect'. The surface on different devices will change by presence of other screen items (title bar, rotation, ...). You have to pick the closest ratio and position the preview on the screen so it either overflows on sides or leaves (black) strips on the sides (center it). Look at this GitHub exercise where I tried to tackle the problem. The best you can do is to let the preview 'overflow' the display surface.

Also, the picture you get after 'takePicture' has again different size and ratio. There is a way to 'crop' the resulting picture to the ratio of the preview, if that is what you want (the example I mention does it differently). Still, the problem you are trying to tackle can not be solved perfectly. For instance, if your surface area is 1000x800 and your closest available preview ratio is 1200x800, you have to select that one and set 'fit-in' here to false. Now you'll see cropped portion of selected preview size (100pixs overflowing on each size, but who cares, it is not visible).

When you get the picture later, again, you have to select he size that suits your needs. Not only the closest ratio, but also the picture size itself. For instance if you got picture size 2000x1500 (as you chose) and you want to keep only the portion visible on the screen, you have to crop it to the 1000x800 ratio of your surface (5:4), resulting in picture 1875x1500. Or, for instance, if you're low on memory and use the picture for upload/viewing, you may shoot for smaller pic size like 800x600 and crop it to 750x600.

Be careful, though, the GitHub example I mentioned goes the other way, it tries to get the final picture of desired size 1600x1200 (ratio 4:3).

I hope I got close enough to help you with something that cannot be answered with simple yes/no. Just the fact that Camera API has so many questions here indicates that the solution is not clean.

Overflowing the bounds of the view seems like a good way to go since there will be no distortion in the image - the only tricky part would be in cropping the final image. In my scenario I have a square FrameLayout that acts as a "mask" that will only let a square portion of the Camera's SurfaceView be visible to the user (think Instagram camera view). It will always be a square so the final image will be screenWidth x screenWidth in dimension. Can you point me in the right direction in how to go about cropping out the image assuming both my preview size and picture size are different?

So I will always display my preview at the device's full resolution but the user will only see a square portion of that.

@MaxWorg Assuming you are running Android Studio, try to pull the GitHub example (I believe FIT_IN is set 'true'), select the preview that is 1:1 (hopefuly the camera has one) and play with this code section: github.com/seanpjanson/AndyCam/blob/master/src/main/java/com/

If you don't find ratio 1.000, select the closest one and mask the surface with a square template (FrameLayout that contains SurfaceView + Imageview with square transparent opening). Sorry I can't help more, busy with completely unrelated Android issues.

java - Correct way to calculate best camera preview size maintaing asp...

java android android-camera aspect-ratio cwac-camera
Rectangle 27 0

I fixed it by removing the following code from the Preview class,

if(mPreviewSize != null) {
    previewWidth = mPreviewSize.width;
    previewHeight = mPreviewSize.height;
}

java - How can I set the camera preview size to fullscreen with Androi...

java android
Rectangle 27 0

I believe I read in a blog that the grayscale data is in the first x*y bytes. Yuv should represent luminance, so the data is there, although it isn't a perfect grayscale. Its great for relative brightness, but not grayscale, as each color isn't as bright as each other in rgb. Green is usually given a stronger weight in luminosity conversions. Hope this helps!

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 0

I believe I read in a blog that the grayscale data is in the first x*y bytes. Yuv should represent luminance, so the data is there, although it isn't a perfect grayscale. Its great for relative brightness, but not grayscale, as each color isn't as bright as each other in rgb. Green is usually given a stronger weight in luminosity conversions. Hope this helps!

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview
Rectangle 27 0

Is this not what you want? Just use a SurfaceView in your layout, then somewhere in your init like onResume():

SurfaceView surfaceView = ...
SurfaceHolder holder = surfaceView.getHolder();
...
Camera camera = ...;
camera.setPreviewDisplay(holder);

It just sends the frames straight to the view as fast as they arrive.

If you want grayscale, modify the camera parameters with setColorEffect("mono").

Thanks, but I really need to use callback and display images manually. I don't need only grayscale image, I'm using some more filters, that I didn't mention in my question...

java - Android: how to display camera preview with callback? - Stack O...

java android camera android-camera preview