Hello friends in this blog we are learn about how to use camera
actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Simple Camera"
android:gravity="center_horizontal" />
<ImageView
android:scaleType="fitCenter"
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="400dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/take_picture"
android:layout_margin="5dip"
android:text="Take Picture"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Simple Camera"
android:gravity="center_horizontal" />
<ImageView
android:scaleType="fitCenter"
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="400dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/take_picture"
android:layout_margin="5dip"
android:text="Take Picture"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
ActivityMain.java
package com.samset.simplecameraexample;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private static final int TAKE_PICTURE_CODE = 100;
private Button btncapture;
private Bitmap cameraBitmap = null;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btncapture = ((Button) findViewById(R.id.take_picture));
imageView = (ImageView) findViewById(R.id.img);
btncapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startCamera();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (TAKE_PICTURE_CODE == requestCode) {
processCameraImage(data);
}
}
private void startCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_CODE);
}
private void processCameraImage(Intent intent) {
cameraBitmap = (Bitmap) intent.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
}
}
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private static final int TAKE_PICTURE_CODE = 100;
private Button btncapture;
private Bitmap cameraBitmap = null;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btncapture = ((Button) findViewById(R.id.take_picture));
imageView = (ImageView) findViewById(R.id.img);
btncapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startCamera();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (TAKE_PICTURE_CODE == requestCode) {
processCameraImage(data);
}
}
private void startCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_CODE);
}
private void processCameraImage(Intent intent) {
cameraBitmap = (Bitmap) intent.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
}
}
AndroidMainifast.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.samset.simplecameraexample">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.samset.simplecameraexample">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you
FullSourceCodeSimpleCameraSample
Live Sample
No comments:
Post a Comment