How to obtain bitmap from data in onPreviewFrame method of previewCallback and display on surface using canvas

by loril...@gmail.com » Sun, 17 May 2009 11:20:15 GMT


Sponsored Links
 Hi there,

I need to capture an image from camera and display on surfaceview for
my project. I am trying to obtain bitmap from data in onPreviewFrame
method of previewCallback and display on surface using canvas.

Can someone please help me?

Following steps were done:

- created a Preview class extending SurfaceView and implementing
SurfaceHolder.Callback set it as the content of activity.

- installed a SurfaceHolder.Callback in my Preview to get notified
when the underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
//mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

- started a thread in surfaceCreated method described as follows:

public void run() {
                                // Open the Camera
                                camera = Camera.open();

                                if (camera != null) {
                                         try {
                                                
camera.setPreviewDisplay(mHolder);
                                         } catch (IOException e) {
                                                        // TODO Auto-generated 
catch block
                                                        e.printStackTrace();
                                        }

                                camera.startPreview();
                                ImageCaptureCallback iccb = new 
ImageCaptureCallback ();
                                camera.setPreviewCallback(iccb);
                                }


----------


public class ImageCaptureCallback implements PreviewCallback {

                public void onPreviewFrame(byte[] data, Camera camera) {
               //ToDo
            }


Please help. I can explain my problem in detail if anybody wish to
solve it....

--~--~---------~--~----~------------~-------~--~----~



How to obtain bitmap from data in onPreviewFrame method of previewCallback and display on surface using canvas

by newbyca » Wed, 01 Jul 2009 23:00:47 GMT


 i'm working with the camera preview in my current project ... one
thing that has helped me out *a lot* is the zebra crossing (zx) code:

 http://code.google.com/p/zxing/source/browse/ 

specifically, zx parses a small region from the preview data to
determine if it contains a barcode. if a barcode is found, the region
is then passed around as a bitmap ... so your answer is *kinda* there.
i say *kinda* because its a little more complicated than that, and too
complicated to just post a code snippet ... or at least it is as far
as I know :)

one thing you'll notice from the zx code is that the preview data is
not RGB color encoded. that is, if you read the preview data byte for
byte, it won't be laid out in simple frames of RGB triplets. in fact,
their code assumes the preview data is encoded using a YUV scheme
( http://en.wikipedia.org/wiki/YUV ). this is normal for video\camera
equipment and a safe assumption. but the reason i bring it up is that
if you want to draw a bitmap using preview data, you must do it in
terms of RGB. i.e., you will have to translate from YUV to RGB at some
point. this translation isn't too hard tho ... the wiki contains the
formulas.

HTH//




--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


How to obtain bitmap from data in onPreviewFrame method of previewCallback and display on surface using canvas

by newbyca » Thu, 02 Jul 2009 06:15:46 GMT


 ere's an example of the YUV conversion:
http://blog.tomgibara.com/post/132956174/yuv420-to-rgb565-conversion-in-android


On Jul 1, 2:30am, newbyca <newb...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



How to obtain bitmap from data in onPreviewFrame method of previewCallback and display on surface using canvas

by lori » Sat, 04 Jul 2009 17:23:02 GMT


 hanks for the reply,

I tried to convert the YUV data in first preview frame in
onPreviewFrame() to RGB and then convert it to bitmap using the YUV
conversion provided.
But I get an out of memory issue within a second. Can you please help
me out?



On Jul 2, 3:15am, newbyca <newb...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~



How to obtain bitmap from data in onPreviewFrame method of previewCallback and display on surface using canvas

by newbyca » Sat, 11 Jul 2009 07:48:55 GMT


 ou need to tell me a little more about the issue ... but my guess is
you were seeing something like "Bitmap to big for allocated memory" or
something or other? i forgot exactly, but i've seen an error like that
before trying to write to bitmaps. again, this is just a guess, but
are you creating a new Bitmap object in each call to onPreviewFrame?
If so, that's a no-no :)

Instead, make your Bitmap class scoped and flush it out each time.

hth, chris

On Jul 4, 5:22am, lori <loril...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~