Today I'am learning about using camera in android and its attributes and codes, lets get started.
There are two main methods that are used while using a camera in app.
1). Direct approach (i.e image is directly saved to a image view or a image button or background without saving it to the internal or external storage in the phone or external storage).
2). Indirect approach ( in which the image is firstly stored in internal or external storage and then acceded by the app contents).
The first approach is very simple and easy to implement which is as follows:-
Firstly set a button or any other onCLick attribute to any thing that may open a camera like I'am using onClick attribute in an Button. e.g
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/icon"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:onClick="camera"
android:text="camera"/>
After the button is set and is accessed in java activity the following code can be applied to it.
//Declare the int variable in the top
final static int cameraData = 1999;
// declare the imageView or any other thing that may contain image.
ImageView iv;
//button on clicked
public void camera(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, cameraData);
//accessing the data
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
iv = (ImageView) findViewById(R.id.imageView)
iv.setImageBitmap(bmp);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
}
The second approach is similar to first one but includes some additional steps like:
//declaring image path
String cameraPath= Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";
public void camera(View v) {
File cameraFile = new File(imageFilePath);
Uri cameraUri= Uri.fromFile(imageFile);
Intent in = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
in.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, cameraUri);
startActivityForResult(in, cameraData);
// and receive the image as
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_OK) {
BitmapFactory.Options bmpFactory = new BitmapFactory.Options();
bmpFactory.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(ImageFilePath, bmpFactory);
iv = (ImageView) findViewById(R.id.imageView)
iv.setImageBitmap(bmp);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
}
and this is how it is done.
There are two main methods that are used while using a camera in app.
1). Direct approach (i.e image is directly saved to a image view or a image button or background without saving it to the internal or external storage in the phone or external storage).
2). Indirect approach ( in which the image is firstly stored in internal or external storage and then acceded by the app contents).
The first approach is very simple and easy to implement which is as follows:-
Firstly set a button or any other onCLick attribute to any thing that may open a camera like I'am using onClick attribute in an Button. e.g
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/icon"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:onClick="camera"
android:text="camera"/>
After the button is set and is accessed in java activity the following code can be applied to it.
//Declare the int variable in the top
final static int cameraData = 1999;
// declare the imageView or any other thing that may contain image.
ImageView iv;
//button on clicked
public void camera(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, cameraData);
//accessing the data
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
iv = (ImageView) findViewById(R.id.imageView)
iv.setImageBitmap(bmp);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
}
The second approach is similar to first one but includes some additional steps like:
//declaring image path
String cameraPath= Environment.getExternalStorageDirectory().getAbsolutePath() + "/picture.jpg";
public void camera(View v) {
File cameraFile = new File(imageFilePath);
Uri cameraUri= Uri.fromFile(imageFile);
Intent in = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
in.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, cameraUri);
startActivityForResult(in, cameraData);
// and receive the image as
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_OK) {
BitmapFactory.Options bmpFactory = new BitmapFactory.Options();
bmpFactory.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(ImageFilePath, bmpFactory);
iv = (ImageView) findViewById(R.id.imageView)
iv.setImageBitmap(bmp);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
}
and this is how it is done.
No comments:
Post a Comment