Android - Simple Image Capture

Submitted by: 
Operating System: 
Visitors have accessed this post 1391 times.

In this tutorial we will try to create a Simple Image Capture using Android. This simple application can be used to any system that needed the information about the location of the user. The android is an open source operating system it's free and user friendly to mobile developers. Android is available to any devices such as TV, phones, watches etc. So now let's do the coding.....

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simpleimagecapture.MainActivity">
  11.  
  12. <ImageView
  13. android:id="@+id/iv_image"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:layout_above="@+id/btn_capture"
  17. app:srcCompat="@mipmap/ic_launcher" />
  18.  
  19. android:id="@+id/btn_capture"
  20. android:layout_height="wrap_content"
  21. android:layout_width="wrap_content"
  22. android:text="Capture"
  23. android:layout_marginBottom="30dp"
  24. android:layout_alignParentBottom="true"
  25. android:layout_centerHorizontal="true" />
  26. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simpleimagecapture">
  5.  
  6. <uses-feature android:name="android.hardware.camera2" android:required="true"/>
  7.  
  8. <application
  9. android:allowBackup="true"
  10. android:icon="@mipmap/ic_launcher"
  11. android:label="@string/app_name"
  12. android:roundIcon="@mipmap/ic_launcher_round"
  13. android:supportsRtl="true"
  14. android:theme="@style/AppTheme">
  15. <activity android:name=".MainActivity"
  16. android:configChanges="orientation"
  17. android:screenOrientation="portrait">
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20.  
  21. <category android:name="android.intent.category.LAUNCHER" />
  22. </intent-filter>
  23. </activity>
  24. </application>
  25. </manifest>

The Main Function

This code contains the main function of the application. This code will force open the built camera to take a shot and display it in the ImageView at the same time. To start with first locate your MainActivity java file and open it, then write these variable inside the MainActivity class.

  1. Button btn_capture;
  2. ImageView iv_image;
  3. int code = 1;

Then write these method to make to code work correctly.

  1. public void onActivityResult(int code, int resultCode, Intent data) {
  2. if(code == this.code){
  3. if(resultCode == RESULT_OK){
  4. Bundle bundle = new Bundle();
  5. bundle = data.getExtras();
  6. Bitmap bmp;
  7. bmp = (Bitmap)bundle.get("data");
  8. iv_image.setImageBitmap(bmp);
  9. }
  10. }
  11. }

Finally, initialize the require methods inside the onCreate method to run the application.

  1. btn_capture = (Button)findViewById(R.id.btn_capture);
  2. iv_image = (ImageView)findViewById(R.id.iv_image);
  3.  
  4.  
  5.  
  6. btn_capture.setOnClickListener(new View.OnClickListener() {
  7. @Override
  8. public void onClick(View v) {
  9. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  10. if(intent.resolveActivity(getPackageManager()) != null){
  11. startActivityForResult(intent, code);
  12. }
  13. }
  14. });

Try to run the app and see if it worked.

There you have it we have created a Simple Image Capture using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.