Introduction

Today I will share a new article, this is on the camera in Android.
This article shows how to make a custom camera in Android and how to save the image in the gallery that's captured by the custom camera.
For this first off make the Layout XML file in the resource folder. The code is shown below.
  1. camerademo_activity.xml
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" >
  6. <SurfaceView
  7. android:id="@+id/surfaceView"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent" />
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_centerInParent="true"
  14. android:text="Camera Demo application\nDeveloped by Ravi Sharma"
  15. android:textColor="@android:color/black" />
  16. <Button
  17. android:id="@+id/captureImage"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_alignParentBottom="true"
  21. android:layout_centerHorizontal="true"
  22. android:layout_marginBottom="@dimen/activity_vertical_margin"
  23. android:background="@drawable/btn_broadcast_selected" />
  24. <Button
  25. android:id="@+id/flash"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_alignParentBottom="true"
  29. android:layout_marginBottom="@dimen/activity_vertical_margin"
  30. android:layout_marginRight="25dp"
  31. android:layout_toLeftOf="@id/captureImage"
  32. android:background="@drawable/btn_flash" />
  33. <Button
  34. android:id="@+id/flipCamera"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_alignParentBottom="true"
  38. android:layout_marginBottom="@dimen/activity_vertical_margin"
  39. android:layout_marginLeft="25dp"
  40. android:layout_toRightOf="@id/captureImage"
  41. android:background="@drawable/btn_flipcamera" />
  42. </RelativeLayout>
In the XML file there are the following three buttons:
The surface view shows the camera to be displayed on the screen.
For this make a CameraDemoActivity.java class and extend it with activity and override the onCreate method and make the object of all the Buttons and SurfaceView.
  1. public class CameraDemoActivity extends Activity{
  2. private SurfaceView surfaceView;
  3. private SurfaceHolder surfaceHolder;
  4. private Camera camera;
  5. private Button flipCamera;
  6. private Button flashCameraButton;
  7. private Button captureImage;
  8. private int cameraId;
  9. private boolean flashmode = false;
  10. private int rotation;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.camerademo_activity);
  15. // camera surface view created
  16. cameraId = CameraInfo.CAMERA_FACING_BACK;
  17. flipCamera = (Button) findViewById(R.id.flipCamera);
  18. flashCameraButton = (Button) findViewById(R.id.flash);
  19. captureImage = (Button) findViewById(R.id.captureImage);
  20. surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
  21. }
  22. }
After this make the SurfaceHolder object and add the callback with SurfaceHolder and add the listener with a button. For doing that you must implement the onClickListener and Callback (for SurfaceHolder).
  1. public class CameraDemoActivity extends Activity implements Callback,
  2. OnClickListener
And put the code in the onCreate method below the earlier code.
  1. surfaceHolder = surfaceView.getHolder();
  2. surfaceHolder.addCallback(this);
  3. flipCamera.setOnClickListener(this);
  4. captureImage.setOnClickListener(this);
  5. flashCameraButton.setOnClickListener(this);
  6. getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
The last line of the preceding code is used to keep the screen on until the activity is running.
Now we need to check the number of the camera in the device and check whether or not the device has a flash.
Use the following code in the onCreate method.
  1. if (Camera.getNumberOfCameras() > 1) {
  2. flipCamera.setVisibility(View.VISIBLE);
  3. }
  4. if (!getBaseContext().getPackageManager().hasSystemFeature(
  5. PackageManager.FEATURE_CAMERA_FLASH)) {
  6. flashCameraButton.setVisibility(View.GONE);
  7. }
After adding the callback on the surfaceHolder object you should override the three methods.
All three are below.
  1. @Override
  2. public void surfaceCreated(SurfaceHolder holder) {
  3. }
  4. @Override
  5. public void surfaceChanged(SurfaceHolder holder, int format, int width,
  6. int height) {
  7. }
  8. @Override
  9. public void surfaceDestroyed(SurfaceHolder holder) {
  10. }
In all these three methods we only used the surfaceCreated method as in the following:
  1. @Override
  2. public void surfaceCreated(SurfaceHolder holder) {
  3. if (!openCamera(CameraInfo.CAMERA_FACING_BACK)) {
  4. alertCameraDialog ();
  5. }
  6. }
There are two methods. One is to open the camera and the second is alertCameraDialog. The openCameera method opens the camera and if there is an issue then alertCameraDialog is called.
First off all discuss the alertCameraDialog method. This method is only used to show the message “error to open camera”.
  1. private void alertCameraDialog() {
  2. AlertDialog.Builder dialog = createAlert(CameraDemoActivity.this,
  3. "Camera info", "error to open camera");
  4. dialog.setNegativeButton("OK", new DialogInterface.OnClickListener() {
  5. @Override
  6. public void onClick(DialogInterface dialog, int which) {
  7. dialog.cancel();
  8. }
  9. });
  10. dialog.show();
  11. }
Now focus on the open camera method as in the following:
  1. private boolean openCamera(int id) {
  2. boolean result = false;
  3. cameraId = id;
  4. releaseCamera();
  5. try {
  6. camera = Camera.open(cameraId);
  7. } catch (Exception e) {
  8. e.printStackTrace();
  9. }
  10. if (camera != null) {
  11. try {
  12. setUpCamera(camera);
  13. camera.setErrorCallback(new ErrorCallback() {
  14. @Override
  15. public void onError(int error, Camera camera) {
  16. //to show the error message.
  17. }
  18. });
  19. camera.setPreviewDisplay(surfaceHolder);
  20. camera.startPreview();
  21. result = true;
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. result = false;
  25. releaseCamera();
  26. }
  27. }
  28. return result;
  29. }
In this method we get the cameraid as a parameter and pass it to open the camera.
There is another method to release the camera, in other words, stop whichever camera is running (back or front) and make the camera object null for further use.
  1. private void releaseCamera() {
  2. try {
  3. if (camera != null) {
  4. camera.setPreviewCallback(null);
  5. camera.setErrorCallback(null);
  6. camera.stopPreview();
  7. camera.release();
  8. camera = null;
  9. }
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. Log.e("error", e.toString());
  13. camera = null;
  14. }
  15. }
Another important method in this is setUpCamera that gets the camera object as a parameter. In this method we manage the rotation and the flash button also because the front camera doesn't support a flash.
  1. private void setUpCamera(Camera c) {
  2. Camera.CameraInfo info = new Camera.CameraInfo();
  3. Camera.getCameraInfo(cameraId, info);
  4. rotation = getWindowManager().getDefaultDisplay().getRotation();
  5. int degree = 0;
  6. switch (rotation) {
  7. case Surface.ROTATION_0:
  8. degree = 0;
  9. break;
  10. case Surface.ROTATION_90:
  11. degree = 90;
  12. break;
  13. case Surface.ROTATION_180:
  14. degree = 180;
  15. break;
  16. case Surface.ROTATION_270:
  17. degree = 270;
  18. break;
  19. default:
  20. break;
  21. }
  22. if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
  23. // frontFacing
  24. rotation = (info.orientation + degree) % 330;
  25. rotation = (360 - rotation) % 360;
  26. } else {
  27. // Back-facing
  28. rotation = (info.orientation - degree + 360) % 360;
  29. }
  30. c.setDisplayOrientation(rotation);
  31. Parameters params = c.getParameters();
  32. showFlashButton(params);
  33. List<String> focusModes = params.getSupportedFlashModes();
  34. if (focusModes != null) {
  35. if (focusModes
  36. .contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
  37. params.setFlashMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
  38. }
  39. }
  40. params.setRotation(rotation);
  41. }
To manage the flash there is a method “showFlashButton(params)”.
  1. private void showFlashButton(Parameters params) {
  2. boolean showFlash = (getPackageManager().hasSystemFeature(
  3. PackageManager.FEATURE_CAMERA_FLASH) && params.getFlashMode() != null)
  4. && params.getSupportedFlashModes() != null
  5. && params.getSupportedFocusModes().size() > 1;
  6. flashCameraButton.setVisibility(showFlash ? View.VISIBLE
  7. : View.INVISIBLE);
  8. }
Override on the onClick(View v) method.
And use the code below:
  1. @Override
  2. public void onClick(View v) {
  3. switch (v.getId()) {
  4. case R.id.flash:
  5. flashOnButton();
  6. break;
  7. case R.id.flipCamera:
  8. flipCamera();
  9. break;
  10. case R.id.captureImage:
  11. takeImage();
  12. break;
  13. default:
  14. break;
  15. }
  16. }
There are three options in the onClick method, Flash, Flip camera and click the image.
The following code is for the flash on the camera.
  1. private void flashOnButton() {
  2. if (camera != null) {
  3. try {
  4. Parameters param = camera.getParameters();
  5. param.setFlashMode(!flashmode ? Parameters.FLASH_MODE_TORCH
  6. : Parameters.FLASH_MODE_OFF);
  7. camera.setParameters(param);
  8. flashmode = !flashmode;
  9. } catch (Exception e) {
  10. // TODO: handle exception
  11. }
  12. }
  13. }
And the Flip Camera method is given below:
  1. private void flipCamera() {
  2. int id = (cameraId == CameraInfo.CAMERA_FACING_BACK ? CameraInfo.CAMERA_FACING_FRONT
  3. : CameraInfo.CAMERA_FACING_BACK);
  4. if (!openCamera(id)) {
  5. alertCameraDialog();
  6. }
  7. }
Then the following is the main method of the activity to capture the image.
  1. private void takeImage() {
  2. camera.takePicture(null, null, new PictureCallback() {
  3. private File imageFile;
  4. @Override
  5. public void onPictureTaken(byte[] data, Camera camera) {
  6. try {
  7. // convert byte array into bitmap
  8. Bitmap loadedImage = BitmapFactory.decodeByteArray(data, 0,
  9. data.length);
  10. // rotate Image
  11. Matrix rotateMatrix = new Matrix();
  12. rotateMatrix.postRotate(rotation);
  13. Bitmap rotatedBitmap = Bitmap.createBitmap(loadedImage, 0,
  14. 0, loadedImage.getWidth(), loadedImage.getHeight(),
  15. rotateMatrix, false);
  16. String state = Environment.getExternalStorageState();
  17. File folder = null;
  18. if (state.contains(Environment.MEDIA_MOUNTED)) {
  19. folder = new File(Environment
  20. .getExternalStorageDirectory() + "/Demo");
  21. } else {
  22. folder = new File(Environment
  23. .getExternalStorageDirectory() + "/Demo");
  24. }
  25. boolean success = true;
  26. if (!folder.exists()) {
  27. success = folder.mkdirs();
  28. }
  29. if (success) {
  30. java.util.Date date = new java.util.Date();
  31. imageFile = new File(folder.getAbsolutePath()
  32. + File.separator
  33. + new Timestamp(date.getTime()).toString()
  34. + "Image.jpg");
  35. imageFile.createNewFile();
  36. } else {
  37. Toast.makeText(getBaseContext(), "Image Not saved",
  38. Toast.LENGTH_SHORT).show();
  39. return;
  40. }
  41. ByteArrayOutputStream ostream = new ByteArrayOutputStream();
  42. // save image into gallery
  43. rotatedBitmap.compress(CompressFormat.JPEG, 100, ostream);
  44. FileOutputStream fout = new FileOutputStream(imageFile);
  45. fout.write(ostream.toByteArray());
  46. fout.close();
  47. ContentValues values = new ContentValues();
  48. values.put(Images.Media.DATE_TAKEN,
  49. System.currentTimeMillis());
  50. values.put(Images.Media.MIME_TYPE, "image/jpeg");
  51. values.put(MediaStore.MediaColumns.DATA,
  52. imageFile.getAbsolutePath());
  53. CameraDemoActivity.this.getContentResolver().insert(
  54. Images.Media.EXTERNAL_CONTENT_URI, values);
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. });
  60. }
Now we need to add some permission in the manifest file.
These are:
  1. <uses-feature android:name="android.hardware.camera" />
  2. <uses-permission android:name="android.permission.CAMERA" />
  3. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  4. <uses-permission
  5. android:name="android.permission.FLASHLIGHT"
  6. android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
  7. android:protectionLevel="normal" />
Some devices show the out of memory error. To avoid this add this line in the application tag:
  1. <application
  2. .
  3. .
  4. .
  5. android:largeHeap="true"
  6. android:theme="@style/AppTheme" >
The output of the application looks like this:
custom camara
The complete code of the entire Activity isthe following.
  1. package com.example.customcamera;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.sql.Timestamp;
  7. import java.util.List;
  8. import android.app.Activity;
  9. import android.app.AlertDialog;
  10. import android.app.AlertDialog.Builder;
  11. import android.content.ContentValues;
  12. import android.content.Context;
  13. import android.content.DialogInterface;
  14. import android.content.pm.PackageManager;
  15. import android.graphics.Bitmap;
  16. import android.graphics.Bitmap.CompressFormat;
  17. import android.graphics.BitmapFactory;
  18. import android.graphics.Matrix;
  19. import android.hardware.Camera;
  20. import android.hardware.Camera.CameraInfo;
  21. import android.hardware.Camera.ErrorCallback;
  22. import android.hardware.Camera.Parameters;
  23. import android.hardware.Camera.PictureCallback;
  24. import android.os.Bundle;
  25. import android.os.Environment;
  26. import android.provider.MediaStore;
  27. import android.provider.MediaStore.Images;
  28. import android.util.Log;
  29. import android.view.ContextThemeWrapper;
  30. import android.view.Surface;
  31. import android.view.SurfaceHolder;
  32. import android.view.SurfaceHolder.Callback;
  33. import android.view.SurfaceView;
  34. import android.view.View;
  35. import android.view.View.OnClickListener;
  36. import android.view.WindowManager;
  37. import android.widget.Button;
  38. import android.widget.Toast;
  39. public class CameraDemoActivity extends Activity implements Callback,
  40. OnClickListener {
  41. private SurfaceView surfaceView;
  42. private SurfaceHolder surfaceHolder;
  43. private Camera camera;
  44. private Button flipCamera;
  45. private Button flashCameraButton;
  46. private Button captureImage;
  47. private int cameraId;
  48. private boolean flashmode = false;
  49. private int rotation;
  50. @Override
  51. protected void onCreate(Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. setContentView(R.layout.camerademo_activity);
  54. // camera surface view created
  55. cameraId = CameraInfo.CAMERA_FACING_BACK;
  56. flipCamera = (Button) findViewById(R.id.flipCamera);
  57. flashCameraButton = (Button) findViewById(R.id.flash);
  58. captureImage = (Button) findViewById(R.id.captureImage);
  59. surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
  60. surfaceHolder = surfaceView.getHolder();
  61. surfaceHolder.addCallback(this);
  62. flipCamera.setOnClickListener(this);
  63. captureImage.setOnClickListener(this);
  64. flashCameraButton.setOnClickListener(this);
  65. getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  66. if (Camera.getNumberOfCameras() > 1) {
  67. flipCamera.setVisibility(View.VISIBLE);
  68. }
  69. if (!getBaseContext().getPackageManager().hasSystemFeature(
  70. PackageManager.FEATURE_CAMERA_FLASH)) {
  71. flashCameraButton.setVisibility(View.GONE);
  72. }
  73. }
  74. @Override
  75. public void surfaceCreated(SurfaceHolder holder) {
  76. if (!openCamera(CameraInfo.CAMERA_FACING_BACK)) {
  77. alertCameraDialog();
  78. }
  79. }
  80. private boolean openCamera(int id) {
  81. boolean result = false;
  82. cameraId = id;
  83. releaseCamera();
  84. try {
  85. camera = Camera.open(cameraId);
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. if (camera != null) {
  90. try {
  91. setUpCamera(camera);
  92. camera.setErrorCallback(new ErrorCallback() {
  93. @Override
  94. public void onError(int error, Camera camera) {
  95. }
  96. });
  97. camera.setPreviewDisplay(surfaceHolder);
  98. camera.startPreview();
  99. result = true;
  100. } catch (IOException e) {
  101. e.printStackTrace();
  102. result = false;
  103. releaseCamera();
  104. }
  105. }
  106. return result;
  107. }
  108. private void setUpCamera(Camera c) {
  109. Camera.CameraInfo info = new Camera.CameraInfo();
  110. Camera.getCameraInfo(cameraId, info);
  111. rotation = getWindowManager().getDefaultDisplay().getRotation();
  112. int degree = 0;
  113. switch (rotation) {
  114. case Surface.ROTATION_0:
  115. degree = 0;
  116. break;
  117. case Surface.ROTATION_90:
  118. degree = 90;
  119. break;
  120. case Surface.ROTATION_180:
  121. degree = 180;
  122. break;
  123. case Surface.ROTATION_270:
  124. degree = 270;
  125. break;
  126. default:
  127. break;
  128. }
  129. if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
  130. // frontFacing
  131. rotation = (info.orientation + degree) % 330;
  132. rotation = (360 - rotation) % 360;
  133. } else {
  134. // Back-facing
  135. rotation = (info.orientation - degree + 360) % 360;
  136. }
  137. c.setDisplayOrientation(rotation);
  138. Parameters params = c.getParameters();
  139. showFlashButton(params);
  140. List<String> focusModes = params.getSupportedFlashModes();
  141. if (focusModes != null) {
  142. if (focusModes
  143. .contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
  144. params.setFlashMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
  145. }
  146. }
  147. params.setRotation(rotation);
  148. }
  149. private void showFlashButton(Parameters params) {
  150. boolean showFlash = (getPackageManager().hasSystemFeature(
  151. PackageManager.FEATURE_CAMERA_FLASH) && params.getFlashMode() != null)
  152. && params.getSupportedFlashModes() != null
  153. && params.getSupportedFocusModes().size() > 1;
  154. flashCameraButton.setVisibility(showFlash ? View.VISIBLE
  155. : View.INVISIBLE);
  156. }
  157. private void releaseCamera() {
  158. try {
  159. if (camera != null) {
  160. camera.setPreviewCallback(null);
  161. camera.setErrorCallback(null);
  162. camera.stopPreview();
  163. camera.release();
  164. camera = null;
  165. }
  166. } catch (Exception e) {
  167. e.printStackTrace();
  168. Log.e("error", e.toString());
  169. camera = null;
  170. }
  171. }
  172. @Override
  173. public void surfaceChanged(SurfaceHolder holder, int format, int width,
  174. int height) {
  175. }
  176. @Override
  177. public void surfaceDestroyed(SurfaceHolder holder) {
  178. }
  179. @Override
  180. public void onClick(View v) {
  181. switch (v.getId()) {
  182. case R.id.flash:
  183. flashOnButton();
  184. break;
  185. case R.id.flipCamera:
  186. flipCamera();
  187. break;
  188. case R.id.captureImage:
  189. takeImage();
  190. break;
  191. default:
  192. break;
  193. }
  194. }
  195. private void takeImage() {
  196. camera.takePicture(null, null, new PictureCallback() {
  197. private File imageFile;
  198. @Override
  199. public void onPictureTaken(byte[] data, Camera camera) {
  200. try {
  201. // convert byte array into bitmap
  202. Bitmap loadedImage = null;
  203. Bitmap rotatedBitmap = null;
  204. loadedImage = BitmapFactory.decodeByteArray(data, 0,
  205. data.length);
  206. // rotate Image
  207. Matrix rotateMatrix = new Matrix();
  208. rotateMatrix.postRotate(rotation);
  209. rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,
  210. loadedImage.getWidth(), loadedImage.getHeight(),
  211. rotateMatrix, false);
  212. String state = Environment.getExternalStorageState();
  213. File folder = null;
  214. if (state.contains(Environment.MEDIA_MOUNTED)) {
  215. folder = new File(Environment
  216. .getExternalStorageDirectory() + "/Demo");
  217. } else {
  218. folder = new File(Environment
  219. .getExternalStorageDirectory() + "/Demo");
  220. }
  221. boolean success = true;
  222. if (!folder.exists()) {
  223. success = folder.mkdirs();
  224. }
  225. if (success) {
  226. java.util.Date date = new java.util.Date();
  227. imageFile = new File(folder.getAbsolutePath()
  228. + File.separator
  229. + new Timestamp(date.getTime()).toString()
  230. + "Image.jpg");
  231. imageFile.createNewFile();
  232. } else {
  233. Toast.makeText(getBaseContext(), "Image Not saved",
  234. Toast.LENGTH_SHORT).show();
  235. return;
  236. }
  237. ByteArrayOutputStream ostream = new ByteArrayOutputStream();
  238. // save image into gallery
  239. rotatedBitmap.compress(CompressFormat.JPEG, 100, ostream);
  240. FileOutputStream fout = new FileOutputStream(imageFile);
  241. fout.write(ostream.toByteArray());
  242. fout.close();
  243. ContentValues values = new ContentValues();
  244. values.put(Images.Media.DATE_TAKEN,
  245. System.currentTimeMillis());
  246. values.put(Images.Media.MIME_TYPE, "image/jpeg");
  247. values.put(MediaStore.MediaColumns.DATA,
  248. imageFile.getAbsolutePath());
  249. CameraDemoActivity.this.getContentResolver().insert(
  250. Images.Media.EXTERNAL_CONTENT_URI, values);
  251. } catch (Exception e) {
  252. e.printStackTrace();
  253. }
  254. }
  255. });
  256. }
  257. private void flipCamera() {
  258. int id = (cameraId == CameraInfo.CAMERA_FACING_BACK ? CameraInfo.CAMERA_FACING_FRONT
  259. : CameraInfo.CAMERA_FACING_BACK);
  260. if (!openCamera(id)) {
  261. alertCameraDialog();
  262. }
  263. }
  264. private void alertCameraDialog() {
  265. AlertDialog.Builder dialog = createAlert(CameraDemoActivity.this,
  266. "Camera info", "error to open camera");
  267. dialog.setNegativeButton("OK", new DialogInterface.OnClickListener() {
  268. @Override
  269. public void onClick(DialogInterface dialog, int which) {
  270. dialog.cancel();
  271. }
  272. });
  273. dialog.show();
  274. }
  275. private Builder createAlert(Context context, String title, String message) {
  276. AlertDialog.Builder dialog = new AlertDialog.Builder(
  277. new ContextThemeWrapper(context,
  278. android.R.style.Theme_Holo_Light_Dialog));
  279. dialog.setIcon(R.drawable.ic_launcher);
  280. if (title != null)
  281. dialog.setTitle(title);
  282. else
  283. dialog.setTitle("Information");
  284. dialog.setMessage(message);
  285. dialog.setCancelable(false);
  286. return dialog;
  287. }
  288. private void flashOnButton() {
  289. if (camera != null) {
  290. try {
  291. Parameters param = camera.getParameters();
  292. param.setFlashMode(!flashmode ? Parameters.FLASH_MODE_TORCH
  293. : Parameters.FLASH_MODE_OFF);
  294. camera.setParameters(param);
  295. flashmode = !flashmode;
  296. } catch (Exception e) {
  297. // TODO: handle exception
  298. }
  299. }
  300. }
  301. }
Thanks for reading my article. If anyone has an issue or query about this code then provide it in the comments box.