I'm trying to make an activity that displays 2 instances of the exact same camera input (camera preview) next to each other. I currently have a fragment that displays one instance of camera input in a SurfaceView. The activity layout contains two of these fragments next to each other. The issue is that the left camera preview works correctly, but the right camera preview is just black. During runtime, Camera.open() throws an exception because the camera is already in use (by the other fragment). How do I generate the camera preview once, then copy it, as to avoid the 2 instances fighting over the hardware?
Let me know if you need to see any more code (there isn't really much left). Thank you.
Fragment Layout (fragment_camera.xml):
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jackdelano.capstone.CameraActivity$PlaceholderFragment">
<SurfaceView
xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/myCameraPreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Activity Layout (activity_camera.xml):
<fragment android:name="com.jackdelano.capstone.CameraActivity$CameraFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
tools:layout="@layout/fragment_camera" />
<fragment android:name="com.jackdelano.capstone.CameraActivity$CameraFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
tools:layout="@layout/fragment_camera" />
Fragment Class (part of CameraActivity.java):
public static class CameraFragment extends Fragment {
private SurfaceView myPreview;
private SurfaceHolder myPreviewHolder;
private Camera myCamera;
private boolean isPreviewOn;
private boolean isCameraConfigured;
public CameraFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_camera, container, false);
myCamera = null;
isPreviewOn = false;
isCameraConfigured = false;
myPreview = (SurfaceView)rootView.findViewById(R.id.myCameraPreview);
myPreviewHolder = myPreview.getHolder();
myPreviewHolder.addCallback(myCallback);
return rootView;
}
@Override
public void onResume() {
super.onResume();
try {
myCamera = Camera.open();
}catch(Exception e) {
Log.e(getString(R.string.app_name), "Camera.open threw an exception!");
e.printStackTrace();
}
startPreview();
}
@Override
public void onPause() {
if(isPreviewOn == true){
myCamera.stopPreview();
}
if(myCamera != null) {
myCamera.release();
}
myCamera = null;
isPreviewOn = false;
super.onPause();
}
private Camera.Size determinePreviewSize(int width, int height, Camera.Parameters myParameters) {
Camera.Size returnValue = null;
for(Camera.Size possibleSize : myParameters.getSupportedPreviewSizes()) { //Iterates through potential preview sizes
if(possibleSize.width <= width && possibleSize.height <= height) {
if(returnValue == null) { //If potential preview size is suitable, and there is no current returnValue, it is set as the returnValue
returnValue = possibleSize;
}else if((possibleSize.width*possibleSize.height)>(returnValue.width*returnValue.height)) {
returnValue = possibleSize; //If there is a current returnValue, it checks if new area is larger than old area
}
}
}
return returnValue;
}
private void makePreview(int width, int height) {
if(myCamera != null && myPreviewHolder.getSurface() != null) {
try{
myCamera.setPreviewDisplay(myPreviewHolder);
}
catch(Exception e) {
Log.e(getString(R.string.app_name), "myCamera.setPreviewDisplay(myPreviewHolder) threw an exception!");
e.printStackTrace();
}
if(isCameraConfigured == false) {
Camera.Parameters myCameraParameters = myCamera.getParameters();
Camera.Size myCameraSize = determinePreviewSize(width, height, myCameraParameters);
if(myCameraSize != null) {
myCameraParameters.setPreviewSize(myCameraSize.width, myCameraSize.height);
myCamera.setParameters(myCameraParameters);
isCameraConfigured = true;
}
}
}
}
private void startPreview(){
if(isCameraConfigured == true && myCamera != null) {
myCamera.startPreview();
isPreviewOn = true;
}
}
SurfaceHolder.Callback myCallback = new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {
//Do nuthin' fool
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
makePreview(width, height);
startPreview();
}
public void surfaceDestroyed(SurfaceHolder holder) {
//Do nuthin' fool
}
};
}
}
No comments:
Post a Comment