How to use live camera feed as activity background?

I am trying to create an application in which I need to use live broadcast as a background. (I know that this is a stupid thing, but I can’t help, her client requires).

I tried to do this using SurfaceView , but haven't achieved anything so far.

So far, all that I found during stack overflows is more likely guesses or directions on how to do this, but there are no real-time examples or code help. It would be great if someone who had done this before bought the code with Stack Overflow users like me.

+6
source share
2 answers

Take a look here:

http://developer.android.com/guide/topics/media/camera.html

There is all the code needed to create an activity that shows a camera preview.

+4
source
 package com.example.CameraPreview; import android.content.Context; import android.content.pm.PackageManager; import android.hardware.Camera; import android.util.AttributeSet; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import java.io.IOException; public class CameraView extends SurfaceView implements SurfaceHolder.Callback { private SurfaceHolder mHolder; private Camera mCamera; public CameraView(Context context) { super(context); if(checkCameraHardware(context)) { mCamera = getCameraInstance(); } mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public CameraView(Context context, AttributeSet attrs) { super(context, attrs); if(checkCameraHardware(context)) { mCamera = getCameraInstance(); } mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { // The Surface has been created, now tell the camera where to draw the preview. try { mCamera.setPreviewDisplay(surfaceHolder); mCamera.startPreview(); } catch (IOException e) { Log.d("CameraView", "Error setting camera preview: " + e.getMessage()); } } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) { if (mHolder.getSurface() == null){ return; } try { mCamera.stopPreview(); } catch (Exception e){ } try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (Exception e){ Log.d("CameraView", "Error starting camera preview: " + e.getMessage()); } } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { } private boolean checkCameraHardware(Context context) { if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){ return true; } else { return false; } } public static Camera getCameraInstance(){ Camera c = null; try { c = Camera.open(); } catch (Exception e){ } return c; } } 
+2
source

Source: https://habr.com/ru/post/949311/


All Articles