How to show the camera preview on a SurfaceView?
Solution 1
To show preview from camera using Camera2 API you should do the following steps:
- Get permission to use camera device
- Using CameraManager open connection to a camera
- Have prepared Surface for preview
- Using opened camera device and desired surfaces (it can include not only preview surface) create CaptureSession
- After CaptureSession created you need to create and configure CaptureRequest and submit it to CaptureSession
Need to notice that preparing surfaces and openning connection to camera are independent processes, so you need to be sure, that they are both completed before creating CaptureSession.
Here is the example of activity to display camera preview on the screen:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, Handler.Callback {
static final String TAG = "CamTest";
static final int MY_PERMISSIONS_REQUEST_CAMERA = 1242;
private static final int MSG_CAMERA_OPENED = 1;
private static final int MSG_SURFACE_READY = 2;
private final Handler mHandler = new Handler(this);
SurfaceView mSurfaceView;
SurfaceHolder mSurfaceHolder;
CameraManager mCameraManager;
String[] mCameraIDsList;
CameraDevice.StateCallback mCameraStateCB;
CameraDevice mCameraDevice;
CameraCaptureSession mCaptureSession;
boolean mSurfaceCreated = true;
boolean mIsCameraConfigured = false;
private Surface mCameraSurface = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mSurfaceView = (SurfaceView) findViewById(R.id.SurfaceViewPreview);
this.mSurfaceHolder = this.mSurfaceView.getHolder();
this.mSurfaceHolder.addCallback(this);
this.mCameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE);
try {
mCameraIDsList = this.mCameraManager.getCameraIdList();
for (String id : mCameraIDsList) {
Log.v(TAG, "CameraID: " + id);
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
mCameraStateCB = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
Toast.makeText(getApplicationContext(), "onOpened", Toast.LENGTH_SHORT).show();
mCameraDevice = camera;
mHandler.sendEmptyMessage(MSG_CAMERA_OPENED);
}
@Override
public void onDisconnected(CameraDevice camera) {
Toast.makeText(getApplicationContext(), "onDisconnected", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(CameraDevice camera, int error) {
Toast.makeText(getApplicationContext(), "onError", Toast.LENGTH_SHORT).show();
}
};
}
@Override
protected void onStart() {
super.onStart();
//requesting permission
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
Toast.makeText(getApplicationContext(), "request permission", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "PERMISSION_ALREADY_GRANTED", Toast.LENGTH_SHORT).show();
try {
mCameraManager.openCamera(mCameraIDsList[1], mCameraStateCB, new Handler());
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
@Override
protected void onStop() {
super.onStop();
try {
if (mCaptureSession != null) {
mCaptureSession.stopRepeating();
mCaptureSession.close();
mCaptureSession = null;
}
mIsCameraConfigured = false;
} catch (final CameraAccessException e) {
// Doesn't matter, cloising device anyway
e.printStackTrace();
} catch (final IllegalStateException e2) {
// Doesn't matter, cloising device anyway
e2.printStackTrace();
} finally {
if (mCameraDevice != null) {
mCameraDevice.close();
mCameraDevice = null;
mCaptureSession = null;
}
}
}
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MSG_CAMERA_OPENED:
case MSG_SURFACE_READY:
// if both surface is created and camera device is opened
// - ready to set up preview and other things
if (mSurfaceCreated && (mCameraDevice != null)
&& !mIsCameraConfigured) {
configureCamera();
}
break;
}
return true;
}
private void configureCamera() {
// prepare list of surfaces to be used in capture requests
List<Surface> sfl = new ArrayList<Surface>();
sfl.add(mCameraSurface); // surface for viewfinder preview
// configure camera with all the surfaces to be ever used
try {
mCameraDevice.createCaptureSession(sfl,
new CaptureSessionListener(), null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
mIsCameraConfigured = true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CAMERA:
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
try {
mCameraManager.openCamera(mCameraIDsList[1], mCameraStateCB, new Handler());
} catch (CameraAccessException e) {
e.printStackTrace();
}
break;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCameraSurface = holder.getSurface();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mCameraSurface = holder.getSurface();
mSurfaceCreated = true;
mHandler.sendEmptyMessage(MSG_SURFACE_READY);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mSurfaceCreated = false;
}
private class CaptureSessionListener extends
CameraCaptureSession.StateCallback {
@Override
public void onConfigureFailed(final CameraCaptureSession session) {
Log.d(TAG, "CaptureSessionConfigure failed");
}
@Override
public void onConfigured(final CameraCaptureSession session) {
Log.d(TAG, "CaptureSessionConfigure onConfigured");
mCaptureSession = session;
try {
CaptureRequest.Builder previewRequestBuilder = mCameraDevice
.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
previewRequestBuilder.addTarget(mCameraSurface);
mCaptureSession.setRepeatingRequest(previewRequestBuilder.build(),
null, null);
} catch (CameraAccessException e) {
Log.d(TAG, "setting up preview failed");
e.printStackTrace();
}
}
}
}
Solution 2
With CameraX it's easy now.
// Create a preview use case instance
val preview = Preview.Builder().build()
// Bind the preview use case and other needed user cases to a lifecycle
val camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, preview, imageAnalysis, imageCapture)
// Create a surfaceProvider using the bound camera's cameraInfo
val surfaceProvider = previewView.createSurfaceProvider(camera.cameraInfo)
// Attach the surfaceProvider to the preview use case to start preview
preview.setSurfaceProvider(surfaceProvider)
<androidx.camera.view.PreviewView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:scaleType="fitEnd" />
https://medium.com/androiddevelopers/display-a-camera-preview-with-previewview-86562433d86c
Related videos on Youtube
user2121
Updated on July 05, 2022Comments
-
user2121 almost 2 years
I am trying to open the camera hardware on a SurfaceView. In the layout, I created a SurfaceView and I open the camera as shown in the code below. When I run the code, the toast in the CameraAvailableCB shows up and says "onCameraAvailable" but nothing appears on the SurfaceView.
How do I show the camera display on the SurfaceView?
Code
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.mBtnCapture = (Button) findViewById(R.id.actMain_btn_capture); this.mSurfaceView = (SurfaceView) findViewById(R.id.actMain_surfaceView); this.mSurfaceHolder = this.mSurfaceView.getHolder(); this.mSurfaceHolder.addCallback(this); this.mCameraManager = (CameraManager) this.getSystemService(Context.CAMERA_SERVICE); try { cameraIDsList = this.mCameraManager.getCameraIdList(); for (String id : cameraIDsList) { Log.v(TAG, "CameraID: " + id); } } catch (CameraAccessException e) { e.printStackTrace(); } cameraStateCB = new CameraDevice.StateCallback() { @Override public void onOpened(CameraDevice camera) { Toast.makeText(getApplicationContext(), "onOpened", Toast.LENGTH_SHORT).show(); //requesting permission int permissionCheck = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA); if (permissionCheck != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)) { } else { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA); Toast.makeText(getApplicationContext(), "request permission", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(), "PERMISSION_ALREADY_GRANTED", Toast.LENGTH_SHORT).show(); } //opening the camera try { mCameraManager.openCamera(cameraIDsList[1], cameraStateCB, new Handler()); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onDisconnected(CameraDevice camera) { Toast.makeText(getApplicationContext(), "onDisconnected", Toast.LENGTH_SHORT).show(); } @Override public void onError(CameraDevice camera, int error) { Toast.makeText(getApplicationContext(), "onError", Toast.LENGTH_SHORT).show(); } }; CameraManager.AvailabilityCallback cameraAvailableCB = new CameraManager.AvailabilityCallback() { @Override public void onCameraAvailable(String cameraId) { super.onCameraAvailable(cameraId); Toast.makeText(getApplicationContext(), "onCameraAvailable", Toast.LENGTH_SHORT).show(); } @Override public void onCameraUnavailable(String cameraId) { super.onCameraUnavailable(cameraId); Toast.makeText(getApplicationContext(), "onCameraUnavailable", Toast.LENGTH_SHORT).show(); } }; this.mCameraManager.registerAvailabilityCallback(cameraAvailableCB, new Handler()); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case MY_PERMISSIONS_REQUEST_CAMERA: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) // Open Camera break; } } @Override public void surfaceCreated(SurfaceHolder holder) { Log.w(TAG, "surfaceCreated"); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.w(TAG, "surfaceChanged"); } @Override public void surfaceDestroyed(SurfaceHolder holder) { Log.w(TAG, "surfaceDestroyed"); }
-
Alex Cohn over 7 yearsSo, "onOpened" toast does not appear, too?
-
user2121 over 7 years@AlexCohn some times it appears
-
-
Moses Liao GZ over 6 yearsdo you have this in kotin?
-
Maxim Metelskiy over 6 years@MosesLiaoGZ I am sorry, but no
-
Magnus about 6 yearsThanks, I guess... but it's kind of ridiculous that it takes a huge piece of code like this just to show a freakin' camera preview...
-
Dan about 6 years@BadCash Whilst this is a large amount of code to do a seemingly simple task, there are google made packages which do it simply for you. Have a look at CameraView
-
Hamza Abdullah about 6 yearsthis code Open Front Camera ?? how I can use this to Open back camera
-
Maxim Metelskiy about 6 years@HamzaAbdullah try to change 'mCameraIDsList[1]' to 'mCameraIDsList[0]'
-
Akash Mishra over 5 years"this.mSurfaceHolder = this.mSurfaceView.getHolder();" Attempt to invoke virtual method 'android.view.SurfaceHolder android.view.SurfaceView.getHolder()' on a null object reference
-
Anjani Mittal about 5 yearshey while using this code the camera preview is somewhat elongated, upon orientation changes one can notice that, any idea why its happening ?
-
Shivaraj Patil almost 4 yearsBetter option with cameraX medium.com/androiddevelopers/…
-
ElliotM about 3 yearsThe question asked for rendering on a SurfaceView, not a PreviewView.
-
spartygw almost 3 years@AkashMishra check your layout, probably the problem is your surface view is not named
SurfaceViewPreview
. This linethis.mSurfaceView = (SurfaceView) findViewById(R.id.SurfaceViewPreview);