Saturday, June 9, 2012

Building an Image Editor 1

This post is made to help/support the members of the ADI community who are attending the event Image Editor for Android. The event description is as follows:






Yeah! We are gonna do it step by step. As Step1, we'll try to create an Image Viewer app. The app will call the default inbuilt camera application which lets us capture a pic and then shows the pic in the base/home screen of our app. thats it. simple right?... Lets make an outline of our app.





So, how exactly do we access the default camera application? We'll use an intent to trigger it using the parameter "android.provider.MediaStore.ACTION_IMAGE_CAPTURE". An activity is started with this intent. When this activity ends, the image associated with the intent is obtained. This image is set to the ImageView already created in our layout and displayed in our base screen. We'll start the activity by using StartActivityForResult() method, so that we can fetch the image from the resulting intent. Thats enough about it, lets jump to the code. The layout file consists of just an ImageView component and is embedded below:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!--
ImageView is an in-built android component used for displaying images in our app...
its width and height are set as "fill_parent", so that it'll fill the full window...
its given an unique id
-->
<ImageView
android:id="@+id/returned_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></ImageView>
</LinearLayout>
view raw main.xml hosted with ❤ by GitHub



Now lets look at our Activity:




package com.android.imageProcessing.imageViewer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
public class ImgViewerV1Activity extends Activity
{
ImageView imgView;
// ImageView for displaying an image in our app...
private static final int CAMERA_RESULT = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent imageOpen=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// an intent is created for capturing an image...
startActivityForResult(imageOpen,CAMERA_RESULT);
// the activity is started by passing an intent...
// startActivityForResult() is used whenever an activity return something...
// in this case it returns an image...
}// end of onCreate()
protected void onActivityResult(int requestCode,int resultCode, Intent i)
{
super.onActivityResult(requestCode, resultCode, i);
// if the result is ok...
if(resultCode==RESULT_OK)
{
Bundle extras=i.getExtras();
// the extras associated with the intent is obtained...
Bitmap bm=(Bitmap)extras.get("data");
// The bitmap of the "data" in extras is obtained...
imgView=(ImageView)findViewById(R.id.returned_image_view);
// the ImageView created in the main.xml file is referenced to the variable
// imgView...
imgView.setImageBitmap(bm);
// the bitmap obtained from the intent is assigned to the ImageView...
// in other words the image returned by the intent is copied to imgView...
}
}
}



Check out some of the snaps below:


1. The problem with AVD:




* Note: Click on the picture to view in high resolution.


2. Camera doesn't seem to work:






* Note: Click on the picture to view in high resolution.




3. When uploaded and started in my phone:


















------------> Download Project here <---------------




There are a number of problems in this app like, the picture captured is not saved anywhere, there are no buttons for capturing, no quit button and the image displayed is just a thumbnail of the image captured (low resolution). In the next post, we'll try to capture an image, save it anywhere we want, access it using the file path and we do all this using buttons. Thats it for now. See ya!!!!.....

No comments:

Post a Comment