How do I access the camera on Android phones?

前端 未结 4 1690
庸人自扰
庸人自扰 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: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

提交回复
热议问题