How do I access the camera on Android phones?

前端 未结 4 1685
庸人自扰
庸人自扰 2021-02-13 17:08

I\'ve written a program in Java that takes in an image file and manipulates the image. Now I\'m trying to access the camera so that I can take the photo and give it to the image

相关标签:
4条回答
  • 2021-02-13 17:36

    There are two methods to take photo for your android application

    1)Using Intent

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
    
    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    

    2) Creating a customized camera activity. For that you need following steps

     * Detect and Access Camera
     * Create a Preview Class
     * Build a Preview Layout 
     * Capture and Save Files
     * Release the Camera
    

    You may also refer the following links:

    http://developer.android.com/guide/topics/media/camera.html http://developer.android.com/reference/android/hardware/Camera.html

    0 讨论(0)
  • 2021-02-13 17:36

    the most important method is:

    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] imageData, Camera c) {
    
        }
    };
    

    This method is called when a picture is taken. Here is a good tutorial on this topic: http://www.brighthub.com/mobile/google-android/articles/43414.aspx

    hmm... or maybe you need this one:

    Camera mCamera;
    ...
    public void onClick(View arg0) {
        mCamera.takePicture(null, mPictureCallback, mPictureCallback);
    }
    

    Here is one more example: http://snippets.dzone.com/posts/show/8683

    0 讨论(0)
  • 2021-02-13 17:48

    there are many ways by which u can do this.... One of the better way which i think is the short and simple is to on Button Click u can call intent which opens ur inbuilt camera view... here is the sample code...

    public class CameraDemo extends Activity {
    
    Button ButtonClick;
    int CAMERA_PIC_REQUEST = 2; 
    int  TAKE_PICTURE=0;
    Camera camera;
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        ButtonClick =(Button) findViewById(R.id.Camera);
        ButtonClick.setOnClickListener(new OnClickListener (){
            @Override
            public void onClick(View view)
            {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            }
        });        
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        if( requestCode == CAMERA_PIC_REQUEST)
        {   
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
            image.setImageBitmap(thumbnail);
        }
        else 
        {
            Toast.makeText(demo.this, "Picture NOt taken", Toast.LENGTH_LONG);
        }
    
    }
    }
    

    ..............................................................

    Go through it and, if u have any problem feel free to ask....

    rakesh

    0 讨论(0)
  • 2021-02-13 17:52

    Google is your best friend, here are some tutorials:

    Using the camera

    How-To Program The Google Android Camera To Take Pictures

    Take Picture from Camera Emulator

    camera

    First edit your AndroidManifest.xml, add the camera permission:

    <uses-permission android:name=”android.permission.CAMERA”/>
    

    Camera service has to be opened and closed:

    Camera camera = Camera.open();
     //Do things with the camera
    camera.release();
    

    You can set camera settings, e.g.:

    Camera.Parameters parameters = camera.getParameters();
    parameters.setPictureFormat(PixelFormat.JPEG); 
    camera.setParameters(parameters);
    

    To take a picture:

    private void takePicture() {
      camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
    }
    
    ShutterCallback shutterCallback = new ShutterCallback() {
      public void onShutter() {
        // TODO Do something when the shutter closes.
      }
    };
    
    PictureCallback rawCallback = new PictureCallback() {
      public void onPictureTaken(byte[] _data, Camera _camera) {
        // TODO Do something with the image RAW data.
      }
    };
    
    PictureCallback jpegCallback = new PictureCallback() {
      public void onPictureTaken(byte[] _data, Camera _camera) {
        // TODO Do something with the image JPEG data.
      }
    };
    

    Do not forget to add the camera layout to your main layout xml.

    0 讨论(0)
提交回复
热议问题