Given that the camera is part of the existing hardware in the device, then we need to make permissions to access the camera. To add permissions in android we can add to the AndroidManifest.xml file.
<uses -permission android:name="android.permission.CAMERA"></uses>In simple access to the camera via an intent. If you already have a button with the event being called quite like this:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");With intent as above, then you will directly use the camera, but just open the camera, no additional functions such as preview the result.
startActivity(intent);
Well, the following will also preview the results.
Initially we will determine the layout. Because only simple, then this layout will only use the function keys to access the camera and ImageView to display the camera images.
< ?xml version="1.0" encoding="utf-8"?>Next we determine the content of play ativity like this:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<button android:id="@+id/capture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Take a Picture"></button>
<imageview android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerInside"></imageview>
</linearlayout>
public class MyActivity extends Activity {By coding as above the photo obtained directly displayed on imageView. This can be seen in the code:
private static final int REQUEST_IMAGE = 100;
Button captureButton;
ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
captureButton = (Button)findViewById(R.id.capture);
captureButton.setOnClickListener(listener);
imageView = (ImageView)findViewById(R.id.image);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
//Process and display the image
Bitmap userImage = (Bitmap)data.getExtras().get("data");
imageView.setImageBitmap(userImage);
}
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE);
}
};
}
Bitmap userImage = (Bitmap)data.getExtras().get("data");If you need to determine where the images are stored into the storage, then we need to add Uri destination file, so its code be like:
imageView.setImageBitmap(userImage);
public class MyActivity extends Activity {Completed.
private static final int REQUEST_IMAGE = 100;
Button captureButton;
ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
captureButton = (Button)findViewById(R.id.capture);
captureButton.setOnClickListener(listener);
imageView = (ImageView)findViewById(R.id.image);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
try {
FileInputStream in = new FileInputStream(destination);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10; //Downsample by 10x
Bitmap userImage = BitmapFactory.decodeStream(in, null, options);
imageView.setImageBitmap(userImage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Add extra to save full-image somewhere
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
startActivityForResult(intent, REQUEST_IMAGE);
}
};
}