Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Capture image from Camera Application in Android Programming?

Tags:

android

I am trying to implement Camera application in android,and i got some code from net to create a Live Camera through WebCam.Upto this no problem.Now i have to capture the images when click the button, and i displayed the captured images in the Dialog window.Without any exception the program is running but the captured image is not displayed,some default image is displayed.

My code is

public void captureImage()
{
    Camera.Parameters params = camera.getParameters();
    camera.setParameters(params);
    Camera.PictureCallback jpgCallback = new PictureCallback() 
    {
        public void onPictureTaken(byte[] data, Camera camera) 
        {
            try
            {
                Dialog d=new Dialog(c);
                d.setContentView(0x7f030000);
                BitmapFactory.Options opts = new BitmapFactory.Options();
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,opts);
                TextView tv=(TextView)d.findViewById(0x7f050001);
                ImageView i=(ImageView)d.findViewById(0x7f050000);
                i.setImageBitmap(bitmap);
                tv.setText("Hai"+data.length);
                d.show();
            }
            catch(Exception e)
            {
                AlertDialog.Builder alert=new AlertDialog.Builder(c);
                alert.setMessage("Exception1"+e.getMessage());
                alert.create();
                alert.show();
            }
        }

    };
    camera.takePicture(null, null, jpgCallback);
}

I have no idea from where this default image is coming,i don't how to slove this problem.Anyone knows about this please help me.Waiting for the Reply.....

like image 829
Rajapandian Avatar asked Aug 20 '09 10:08

Rajapandian


People also ask

Which object can be used for capturing an image from the camera in Android?

ACTION_IMAGE_CAPTURE or MediaStore. ACTION_VIDEO_CAPTURE can be used to capture images or videos without directly using the Camera object.

How do you open camera through intent and display captured image?

This is done as follows: Intent camera_intent = new Intent(MediaStore. ACTION_IMAGE_CAPTURE); startActivityForResult(camera_intent, pic_id); Now use the onActivityResult() method to get the result, here is the captured image.


1 Answers

If this is on the emulator, the only available camera image is a default image.

On a completely unrelated note, DO NOT reference resources by raw numbers (e.g., d.setContentView(0x7f030000)). Use the generated R class (e.g., R.layout.something). Those numbers will change, and when they change your application will break.

like image 104
CommonsWare Avatar answered Nov 15 '22 08:11

CommonsWare