Showing posts with label Take picture with camera intent. Show all posts
Showing posts with label Take picture with camera intent. Show all posts

Tuesday, January 8, 2013

Take picture with camera intent


Capture image with camera intent and display in activity:
public class CamActivity extends Activity implements OnClickListener{

            private String imageName;
            private Uri imageUri;
            private static final int IMAGE_CAPTURE = 1;
            private static final int IMAGE_two = 2;
            private static final int IMAGE_three = 3;
            private ImageView pictureHolder;
            private Button removeone;
            private Button takeone;
            private Button removetwo;
            private Button taketwo;
            private Button removethree;
            private Button takethree;
           
            protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.cam);
           
                        removeone = (Button)findViewById(R.id.remove1);
                        removeone.setOnClickListener(this);
                        takeone = (Button)findViewById(R.id.take1);
                        takeone.setOnClickListener(this);
                        removetwo = (Button)findViewById(R.id.remove2);
                        removetwo.setOnClickListener(this);
                        taketwo = (Button)findViewById(R.id.take2);
                        taketwo.setOnClickListener(this);
                        removethree = (Button)findViewById(R.id.remove3);
                        removethree.setOnClickListener(this);
                        takethree = (Button)findViewById(R.id.take3);
                        takethree.setOnClickListener(this);
            }
           
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                        super.onActivityResult(requestCode, resultCode, data);
                        if (resultCode == RESULT_OK){
                                    if(requestCode == IMAGE_CAPTURE)
                                    {
                                                Bundle b = data.getExtras();
                 Bitmap pic = (Bitmap) b.get("data");

                 if (pic != null) {
                     pictureHolder = (ImageView) this.findViewById(R.id.image1);
                     pictureHolder.setImageBitmap(pic);
                     pictureHolder.invalidate();
                                    }
                                                }
                                   
                                    else
                                                if(requestCode == IMAGE_two)
                                                {
                                                             Bundle b = data.getExtras();
                                                             Bitmap pic = (Bitmap)b.get("data");

                             if (pic != null) { 
                                 pictureHolder = (ImageView) this.findViewById(R.id.image2);
                                 pictureHolder.setImageBitmap(pic);
                                 pictureHolder.invalidate();
                                                }
                                                            }
                                                else
                                                            if(requestCode == IMAGE_three)
                                                            {
                                                                         Bundle b = data.getExtras();
                                                                         Bitmap pic = (Bitmap)b.get("data");

                                         if (pic != null) {
                                             pictureHolder = (ImageView) this.findViewById(R.id.image3);
                                             pictureHolder.setImageBitmap(pic);
                                                            }
                                                                        }
                                    }
                        }
           
           
            public void onClick(View v) {
                        switch (v.getId()) {
                        case R.id.take1:
           
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                         imageName =  String.valueOf(System.currentTimeMillis()) + ".jpg";
                          startActivityForResult(intent, IMAGE_CAPTURE);
                       
            break;
                        case R.id.remove1:
// remove the image
                                     pictureHolder = (ImageView) this.findViewById(R.id.image1);
                                     pictureHolder.setImageResource(R.drawable.ic_launcher);
          
                                      break;
                        case R.id.take2:

                                    Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                     imageName = String.valueOf(System.currentTimeMillis()) + ".jpg";
                                      startActivityForResult(intent2, IMAGE_two);
                                      break;
                        case R.id.remove2:
                                     pictureHolder = (ImageView) this.findViewById(R.id.image2);
                                     pictureHolder.setImageResource(R.drawable.ic_launcher);
           
                                      break;
                        case R.id.take3:

                                    Intent intent3 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                      startActivityForResult(intent3, IMAGE_three);
                                      break;
                        case R.id.remove3:
                                     pictureHolder = (ImageView) this.findViewById(R.id.image3);
                                     pictureHolder.setImageResource(R.drawable.ic_launcher);
           
                                      break;
                        default:
                                    break;
                        }
            }
           
}





Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" >

        <LinearLayout
            android:id="@+id/LinearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <RelativeLayout
                android:id="@+id/relativeLayout2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <ImageView
                    android:id="@+id/image1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:src="@drawable/ic_launcher" />

                <LinearLayout
                    android:id="@+id/linearLayout1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:orientation="vertical" >

                    <Button
                        android:id="@+id/remove1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Remove " />

                    <Button
                        android:id="@+id/take1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Take Picture" />
                </LinearLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/relativeLayout3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <ImageView
                    android:id="@+id/image2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:src="@drawable/ic_launcher" />

                <LinearLayout
                    android:id="@+id/linearLayout2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:orientation="vertical" >

                    <Button
                        android:id="@+id/remove2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Remove " />

                    <Button
                        android:id="@+id/take2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Take Picture" />
                </LinearLayout>
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/relativeLayout4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <ImageView
                    android:id="@+id/image3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:src="@drawable/ic_launcher" />

                <LinearLayout
                    android:id="@+id/linearLayout3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:orientation="vertical" >

                    <Button
                        android:id="@+id/remove3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Remove " />

                    <Button
                        android:id="@+id/take3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Take Picture" />
                </LinearLayout>
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>