Android Studio : How to detect camera, activate and capture example
Let's have some fun with Android by learning how to detect the camera, activate the camera and capture with the camera. This app has one button. When pressed, first it will detect if the device has a camera or not. If the device has a camera, it will activate the camera and launch the default Android camera app to capture from the camera.
NOTE: In our example, the camera view is simulated by the Android Virtual Device. You can transfer the APK file over to a real Android device to see how this application works with a real camera.
Here you go!
Fill the XML layout design in the main_activity_camera.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sweetlogic.camera.MainCamera"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="81dp">
<Button
android:id="@+id/buttonActivateCamera"
android:layout_width="300dp"
android:layout_height="50dp"
android:text="Activate Camera"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="42dp"
tools:layout_editor_absoluteY="230dp" />
</android.support.constraint.ConstraintLayout>
Next step is to fill in the Java codes at MainCamera.java
package com.example.sweetlogic.camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Context;
import android.content.pm.PackageManager;
import android.widget.Toast;
import android.util.Log;
import android.provider.MediaStore;
import android.content.Intent;
public class MainCamera extends AppCompatActivity {
// global variable
boolean cameraAvailable = false;
static final int REQUEST_IMAGE_CAPTURE = 1;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_camera);
// listen to activate camera button
addListenerOnActiveCameraButton();
}
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(MainCamera.this, "this device has a camera", Toast.LENGTH_LONG).show();
return true;
} else {
Toast.makeText(MainCamera.this, "no camera on this device", Toast.LENGTH_LONG).show();
return false;
}
}
public void addListenerOnActiveCameraButton() {
Button button = (Button) findViewById(R.id.buttonActivateCamera);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cameraAvailable = checkCameraHardware(context);
if (cameraAvailable) {
// ok, we will use Intent to take photo
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
Log.i("camera", "yes");
} else {
Log.i("camera", "no");
}
}
});
}
}
Hit the Run
button and press the button on your application to activate the camera.
To see your application log messages, on the lower-left corner of the Android Studio, press the Android Monitor
tab and click on the logcat
tab.
References:
https://www.socketloop.com/tutorials/android-studio-image-button-and-button-example
https://developer.android.com/guide/topics/media/camera.html#detect-camera
https://developer.android.com/training/camera/photobasics.html
See also : Android Studio : AlertDialog to get user attention example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+14.2k Golang : GUI with Qt and OpenCV to capture image from camera
+15.6k Golang : Read a file line by line
+8.8k Golang : Intercept and compare HTTP response code example
+5.7k AWS S3 : Prevent Hotlinking policy
+40.7k Golang : How to check if a string contains another sub-string?
+16.1k Golang :Trim white spaces from a string
+15.4k Golang : How to login and logout with JWT example
+13.4k Golang : Convert spaces to tabs and back to spaces example
+7.9k Golang : Reverse text lines or flip line order example
+6.9k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+37k Upload multiple files with Go
+11.5k Golang : convert(cast) float to string