Ok, teman-teman sebelumnya , teman-teman sudah belajar mengenai cara menampilkan kamera di android, namun seperti yang teman2 ketahui, pada part 1 , teman - teman hanya bisa menampilkan kamera saja . Pada part 2 ini kita akan mempelajari, bagaimana cara menyimpan kamera. Ok, buat temen1 siapkan Eclipsnya ya ....^_^. Biar tambah semangat , sambil programming makan cemilan, terus teh hangat atau kopi... mantap deh... ^_^
Ok ,
Tahap Pertama
1. Pada Layout :
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<SurfaceView
android:id="@+id/kameraku"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<SurfaceView
android:id="@+id/kameraku"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
1. Pada AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.irawankamera"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".IrawanKameraActivity"
android:label="@string/app_name" >
<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.irawankamera"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".IrawanKameraActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
3. Pada Program Java
package com.irawankamera;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.content.ContentValues;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class IrawanKameraActivity extends Activity implements SurfaceHolder.Callback, OnClickListener, Camera.PictureCallback {
Camera kamera;
SurfaceHolder surface;
SurfaceView kameraview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
kameraview = (SurfaceView) findViewById(R.id.kameraku);
surface = kameraview.getHolder();
surface.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surface.addCallback(this);
kameraview.setFocusable(true);
kameraview.setFocusableInTouchMode(true);
kameraview.setClickable(true);
kameraview.setOnClickListener(this);
}
@Override
public void onClick(View v ) {
kamera.takePicture(null, null, this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
kamera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
kamera = Camera.open();
try
{
kamera.setPreviewDisplay(holder);
Camera.Parameters params = kamera.getParameters();
if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
params.set("orientation", "portrait");
kamera.setDisplayOrientation(90);
}
else
{
params.set("orientation", "landscape");
kamera.setDisplayOrientation(0);
}
kamera.setParameters(params);
}
catch(IOException ex)
{
Toast t = Toast.makeText(this,ex.getMessage(),Toast.LENGTH_LONG);
kamera.release();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
kamera.stopPreview();
kamera.release();
}
@Override
public void onPictureTaken(byte[] data, Camera kamera) {
Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
try
{
OutputStream imageFileOS = getContentResolver().openOutputStream(imageFileUri);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
}
catch ( FileNotFoundException e)
{
Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
t.show();
}
catch(IOException e)
{
Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
t.show();
}
kamera.startPreview();
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.content.ContentValues;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class IrawanKameraActivity extends Activity implements SurfaceHolder.Callback, OnClickListener, Camera.PictureCallback {
Camera kamera;
SurfaceHolder surface;
SurfaceView kameraview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
kameraview = (SurfaceView) findViewById(R.id.kameraku);
surface = kameraview.getHolder();
surface.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surface.addCallback(this);
kameraview.setFocusable(true);
kameraview.setFocusableInTouchMode(true);
kameraview.setClickable(true);
kameraview.setOnClickListener(this);
}
@Override
public void onClick(View v ) {
kamera.takePicture(null, null, this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
kamera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
kamera = Camera.open();
try
{
kamera.setPreviewDisplay(holder);
Camera.Parameters params = kamera.getParameters();
if(this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
params.set("orientation", "portrait");
kamera.setDisplayOrientation(90);
}
else
{
params.set("orientation", "landscape");
kamera.setDisplayOrientation(0);
}
kamera.setParameters(params);
}
catch(IOException ex)
{
Toast t = Toast.makeText(this,ex.getMessage(),Toast.LENGTH_LONG);
kamera.release();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
kamera.stopPreview();
kamera.release();
}
@Override
public void onPictureTaken(byte[] data, Camera kamera) {
Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
try
{
OutputStream imageFileOS = getContentResolver().openOutputStream(imageFileUri);
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
}
catch ( FileNotFoundException e)
{
Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
t.show();
}
catch(IOException e)
{
Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
t.show();
}
kamera.startPreview();
}
}
Setelah di run, kita lihat hasilnya :
Dan coba lihat di galeri
Ok selesai sudah ya teman2. Untuk source code yang lengkap bisa didownload disini
Jangan Lupa Share ya.... ^_^