Do somthings

Android provides full access to the device camera hardware so you can build a wide range of camera or vision-based apps. Or if you just need a way for the user to capture a photo, you can simply request an existing camera app to capture a photo and return it to you.

Directly controlling a device camera requires a lot more code than requesting pictures or videos from existing camera applications. However, if you want to build a specialized camera application or something fully integrated in your app UI, this post shows you how.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio click File->New->New Project in Android Studio. Note that select Java as the programming language.

Step 2:  First Create a java class name ShowCamera

Navigate to the app > right click project name >New > java Class >ShowCamera

and put this code in there

package com.exampl.xyz;

import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.io.IOException;

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {

private Camera camera;
private SurfaceHolder holder;

public ShowCamera(Context context,Camera camera) {
super(context);
this.camera = camera;
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

@Override
public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (holder.getSurface() == null){
return;
}
// stop preview before making changes
try {
camera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}

// set preview size and make any resize, rotate or
// reformatting changes here

// start preview with new settings
try {
camera.setPreviewDisplay(holder);
camera.startPreview();

} catch (Exception e){
System.out.println("Error starting camera preview: " + e.getMessage());
}
}

@Override
public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

}

@Override
public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
Camera.Parameters parameters = camera.getParameters();

if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
parameters.set("orientation","portrait");
camera.setDisplayOrientation(90);
parameters.setRotation(90);
}else {
parameters.set("orientation","portrait");
camera.setDisplayOrientation(0);
parameters.setRotation(0);
}

camera.setParameters(parameters);
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
}catch (IOException e){
e.printStackTrace();
}


}
}

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add
the below code to that file. Below is the code for the
activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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.allstar.indiavideocall.Activity.CallActivity">

<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>

</FrameLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

Step 5: working with MainActivity.java file.

package com.itfox.shimmerexample;

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Camera;
import android.os.Bundle;
import android.widget.FrameLayout;


public class MainActivity extends AppCompatActivity {

Camera camera;
ShowCamera showCamera;
FrameLayout frameLayout;

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

frameLayout = findViewById(R.id.frameLayout);

//hook's
OpenCamera();
}
private void OpenCamera() {

camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);

//show camera
showCamera = new ShowCamera(this,camera);
frameLayout.frameLayout.addView(showCamera);

}
}

NOTE: if you always want the camera open with the front camera then code:

camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
Categories: Android