Camera Effects In Android Development

This sample application will show camera usage with color effect and white balance effects. When you click on the color effect or white balance button, supported effects will be listed and the selected effect will be set.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it CameraEffects.

2.) Write following into res/values/strings.xml:

 

<resources>

<string name="app_name">CameraEffects</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_camera_effects">CameraEffects</string>
<string name="color_effect">Color Effects</string>
<string name="white_balance">White Balance</string>
<string name="app_desc">This sample camera applicaiton to demonstrate the use of camera effects.</string>
<string name="start_application">Start camera</string>
</resources>

 

 

3.) Write following into 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:background="@android:drawable/screen_background_light"
android:padding="30dip">

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0">

<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/app_desc"
android:gravity="center_horizontal"/>

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_application"
android:id="@+id/start_camera"
android:layout_gravity="center_horizontal"/>
</LinearLayout>

</LinearLayout>

 

 

4.) Write following into AndroidManifest.xml:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cameraeffects"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".CameraEffects"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".CameraActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>

</application>

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/>
</manifest>

 

 

5.) Create and Write following into res/layout/camera.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">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"/>

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/back">

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.7"
android:gravity="top">

<ImageButton
android:id="@+id/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/about"/>
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.3"
android:gravity="bottom">

<ImageButton
android:id="@+id/coloreffect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/coloreffect"/>

<ImageButton
android:id="@+id/whitebalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/whitebalance"/>

<ImageButton
android:id="@+id/takepicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shutter"/>
</LinearLayout>

</LinearLayout>

</LinearLayout>

</LinearLayout>

 

 

6.) Create and write following into src/CameraActivity.java:

 

package com.example.cameraeffects;

import java.io.FileOutputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageButton;

public class CameraActivity extends Activity implements CameraCallback{
private FrameLayout cameraholder = null;
private CameraSurface camerasurface = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);

cameraholder = (FrameLayout)findViewById(R.id.camera_preview);

setupPictureMode();

((ImageButton)findViewById(R.id.takepicture)).setOnClickListener(onButtonClick);
((ImageButton)findViewById(R.id.about)).setOnClickListener(onButtonClick);
((ImageButton)findViewById(R.id.coloreffect)).setOnClickListener(onButtonClick);
((ImageButton)findViewById(R.id.whitebalance)).setOnClickListener(onButtonClick);
}

private void setupPictureMode(){
camerasurface = new CameraSurface(this);

cameraholder.addView(camerasurface, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

camerasurface.setCallback(this);
}

@Override
public void onJpegPictureTaken(byte[] data, Camera camera) {
try
{
FileOutputStream outStream = new FileOutputStream(String.format(
"/sdcard/%d.jpg", System.currentTimeMillis()));

outStream.write(data);
outStream.close();
}
catch(Exception e)
{
e.printStackTrace();
}

camerasurface.startPreview();
}

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
}

@Override
public void onRawPictureTaken(byte[] data, Camera camera) {
}

@Override
public void onShutter() {
}

@Override
public String onGetVideoFilename(){
String filename = String.format("/sdcard/%d.3gp",System.currentTimeMillis());

return filename;
}

private void displayAboutDialog()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle(getString(R.string.app_name));
builder.setMessage("Sample application to demonstrate the use of Camera in Android.");

builder.show();
}

private void displayColorEffectDialog()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle(getString(R.string.color_effect));
builder.setSingleChoiceItems(camerasurface.getSupportedColorEffects(),
camerasurface.getCurrentColorEffect(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
camerasurface.setColorEffect(which);

dialog.dismiss();
}
});

builder.show();
}

private void displayWhiteBalanceDialog()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle(getString(R.string.white_balance));
builder.setSingleChoiceItems(camerasurface.getSupportedWhiteBalances(),
camerasurface.getCurrentWhiteBalance(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
camerasurface.setWhiteBalance(which);

dialog.dismiss();
}
});

builder.show();
}

private View.OnClickListener onButtonClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.about: displayAboutDialog(); break;
case R.id.takepicture: camerasurface.startTakePicture(); break;
case R.id.coloreffect: displayColorEffectDialog(); break;
case R.id.whitebalance: displayWhiteBalanceDialog(); break;
}
}
};
}

 

 

7.) Create and write following into src/CameraSurface.java:

 

package com.example.cameraeffects;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.GestureDetector.OnGestureListener;

public class CameraSurface extends SurfaceView implements SurfaceHolder.Callback, OnGestureListener{
private Camera camera = null;
private SurfaceHolder holder = null;
private CameraCallback callback = null;
private GestureDetector gesturedetector = null;
private String[] supportedColorEffects = null;
private String[] supportedWhiteBalances = null;
private int currentColorEffect = 0;
private int currentWhiteBalance = 0;
private int currentZoom = 0;
private boolean isZoomIn = true;
private boolean isStarted = true;

public CameraSurface(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

initialize(context);
}

public CameraSurface(Context context) {
super(context);

initialize(context);
}

public CameraSurface(Context context, AttributeSet attrs) {
super(context, attrs);

initialize(context);
}

public void setCallback(CameraCallback callback){
this.callback = callback;
}

public void startPreview(){
camera.startPreview();
}

public void startTakePicture(){
camera.autoFocus(new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
takePicture();
}
});
}

public int getCurrentColorEffect(){
return currentColorEffect;
}

public int getCurrentWhiteBalance(){
return currentWhiteBalance;
}

public String[] getSupportedColorEffects(){
return supportedColorEffects;
}

public String[] getSupportedWhiteBalances(){
return supportedWhiteBalances;
}

public void setColorEffect(int effect){
Camera.Parameters parameters = camera.getParameters();

parameters.setColorEffect(supportedColorEffects[effect]);

camera.setParameters(parameters);

currentColorEffect = effect;
}

public void setWhiteBalance(int effect){
Camera.Parameters parameters = camera.getParameters();

parameters.setWhiteBalance(supportedWhiteBalances[effect]);

camera.setParameters(parameters);

currentWhiteBalance = effect;
}

public void takePicture() {
camera.takePicture(
new ShutterCallback() {
@Override
public void onShutter(){
if(null != callback) callback.onShutter();
}
},
new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera){
if(null != callback) callback.onRawPictureTaken(data, camera);
}
},
new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera){
if(null != callback) callback.onJpegPictureTaken(data, camera);
}
});
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
if(null != camera && isStarted)
{
camera.startPreview();
}
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();

try {
camera.setPreviewDisplay(holder);
camera.setPreviewCallback(new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if(null != callback) callback.onPreviewFrame(data, camera);
}
});

final List<String> coloreffects = camera.getParameters().getSupportedColorEffects();
final List<String> whiteBalances = camera.getParameters().getSupportedWhiteBalance();
if(coloreffects != null)
{
supportedColorEffects = new String[coloreffects.size()];
coloreffects.toArray(supportedColorEffects);
}
if(whiteBalances != null)
{
supportedWhiteBalances = new String[whiteBalances.size()];
whiteBalances.toArray(supportedWhiteBalances);
}

} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isStarted = false;

camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();

camera = null;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
return gesturedetector.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e) {
return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}

@Override
public void onLongPress(MotionEvent e) {
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
return false;
}

@Override
public void onShowPress(MotionEvent e) {
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
Camera.Parameters parameters = camera.getParameters();

if(isZoomIn)
currentZoom += 1;
else
currentZoom -= 1;

if(currentZoom > parameters.getMaxZoom())
{
currentZoom = parameters.getMaxZoom();
isZoomIn = false;
}
else if(currentZoom <= 0)
{
currentZoom = 0;
isZoomIn = true;
}

parameters.setZoom(currentZoom);

camera.setParameters(parameters);

Log.i("CameraEffectsDemo", "Current Zoom: " + currentZoom + ", Max Zoom: " + parameters.getMaxZoom());

return false;
}

private void initialize(Context context) {
holder = getHolder();

holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

gesturedetector = new GestureDetector(this);
}
}

 

 

8.) Create and write following into src/CameraCallback.java:

 

package com.example.cameraeffects;

import android.hardware.Camera;

public interface CameraCallback {
public abstract void onPreviewFrame(byte[] data, Camera camera);
public abstract void onShutter();
public abstract void onRawPictureTaken(byte[] data, Camera camera);
public abstract void onJpegPictureTaken(byte[] data, Camera camera);
public abstract String onGetVideoFilename();
}

 

 

9.) Run for output.

Steps:

1.) Create a project named CameraEffects and set the information as stated in the image.

Build Target: Android 4.0
Application Name: CameraEffects
Package Name: com. example. CameraEffects
Activity Name: CameraEffects
Min SDK Version: 2.2

2.) Open CameraEffects.java file and write following code there:

 

package com.example.cameraeffects;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class CameraEffects extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

((Button)findViewById(R.id.start_camera)).setOnClickListener(onButtonClick);
}

private void startCameraActivity() {
Intent intent = new Intent(CameraEffects.this,CameraActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);

finish();
}

private View.OnClickListener onButtonClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.start_camera:
{
startCameraActivity();

break;
}
}
}
};
}

 

 

3.) Compile and build the project.

Note: – For better results check on device.

Output

Leave a Comment:

1 comment
sukh says February 22, 2013

why is there half of the information? no information about the images also are these 2 different projects or same? please reply.

Reply
Add Your Reply