Friday, September 6, 2013
How to capture an image, and display it afterwards
Given that we have a "TakePicture" button which works just like the icon (with photo camera) on our home window in the mobilephone. When we click on that "TakePicture" button, we want our android phone to turn our camera on and show around by the camera. In order to do that , we have an intent as follows ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.takephoto);
Intent intent = new Intent();
// Picture from camera
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
.
.
.
.
Ok now our intent knows that it is going to have a capture action. But before making it happen, we would like to define where we are going to store the image. For this purpose, we create a file with a dynamic name by the help of date object as follows;
Calendar date = Calendar.getInstance();
SimpleDateFormat dateformatter = new SimpleDateFormat
("dd_MM_yyyy_'at'_hh_mm_ss");
//Log.e("timeStamp2: " , ""+dateformatter.format(date.getTime()));
String loggerDate = dateformatter.format(date.getTime());
String newPicFile = "myImageFolder"+ loggerDate + ".jpg";
outPath = "/sdcard/" + newPicFile;
File outFile = new File(outPath);
mCameraFileName = outFile.toString();
outuri = Uri.fromFile(outFile);
And last but not least , inside onClick() function of our button, we put the path info into our intent , and start the action ;
intent.putExtra(MediaStore.EXTRA_OUTPUT, outuri);
startActivityForResult(intent, CAMERA_REQUEST);
That's it , now my app knows that if the user clicks the takePicture button, it will automatically open a new frame where you can take a new picture. And when you take the picture, it enables to you to take a new one , ignore or confirm the capture operation as follows.
And if you would like to do further operation, onActivityResult () method is there to help you.
requestCode is the value I sent above where I called the operation, and the resultCode equals to "RESULT_OK" if the user clicks the check button. In case of such action, I can set the source of an imageView (in the following screen) as the captured image's path. By this way I can show the captured image on my screen. That's it ! Congrats.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
File imgFile = new File(outPath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
}
.
.
.
.
.
.
.
Our variables defined as such ;
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
private Uri outuri ;
private String outPath ="";
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/imageFolder/";
private String selectedImagePath;
final static private int NEW_PICTURE = 1;
private String mCameraFileName;
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment