I'm developing an app that will use the android API camera. However, I will use the camera only for the user to see certain points on a target, ie, it is not necessary to capture photos or videos. The idea is to create a VISUALIZATION DISPLAY .
With the XML layout and the following code I was able to instantiate and display the camera in MainActivity:
XML:
<FrameLayout
android:id="@+id/frameLayoutCamera"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
CLASS MAIN ACTIVITY:
public class MainActivity extends AppCompatActivity {
//Atributos para trabalhar com a camera
private Camera camera;
private FrameLayout frameLayout;
private ShowCamera showCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.frameLayout = (FrameLayout) findViewById(R.id.frameLayoutCamera);
this.camera = Camera.open();
this.showCamera = new ShowCamera(this, camera);//instanciando a classe showCamera
this.frameLayout.addView(showCamera);
}
@Override
protected void onPause() {
super.onPause();
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
}
}
CLASS SHOWCAMERA:
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;
this.holder = getHolder();
this.holder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Camera.Parameters parameters = this.camera.getParameters();
if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE){
parameters.set("orientation", "portrait");
this.camera.setDisplayOrientation(90);
parameters.setRotation(90);
}else {
parameters.set("orientation", "landscape");
this.camera.setDisplayOrientation(0);
parameters.setRotation(0);
}
this.camera.setParameters(parameters);
try {
this.camera.setPreviewDisplay(holder);
this.camera.startPreview();
}catch (IOException e){
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
With the implementations above, I was able to display the camera on the Smartphone screen according to the demo image below:
ButIwantedtostylizethepreviewscreenfortheuserasshownbelow:
Wellthinkingitwouldbesimpletosolve,IcreatedanimagethetemplateIwanted,Iimporteditintothedrawablefolder,andIchangedXMLasfollows,imgaemPNGasbackgroundoftheFrameLayoutcomponent:
<FrameLayoutandroid:id="@+id/frameLayoutCamera"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/display_background"/>
But as a result the app made an error and stopped working "nor did it actually run".
After that, I tried to include an ImageView component overlapping the Camera's FrameLayout component, but it also gave an error:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/display_background"/>
<FrameLayout
android:id="@+id/frameLayoutCamera"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Note: I can put any component overlapping the FrameLayout, Buttons, TextViews, ImageView and others, the application runs normally, but if any component I put an image as background it gives error .. .
Thank you very much if someone can help me, because I am researching the days to see if I find something on the INTERNET to help me, but without success! Until then, I thought it was a simple thing to do, but now it has become difficult.
But I believe that somehow it is possible to do this, display the camera of the phone and put it as if it were a frame in the camera's Preview.
From now on thank you for helping me, hugs!