1

I recently developed a special camera preview, because I needed to unmirror the default preview. The code is as follows:

public class CameraApi extends AppCompatActivity {
    private static final String TAG = "CameraAPI";
    protected TextureView textureView;
    private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
    static {
        ORIENTATIONS.append(Surface.ROTATION_0, 90);
        ORIENTATIONS.append(Surface.ROTATION_90, 0);
        ORIENTATIONS.append(Surface.ROTATION_180, 270);
        ORIENTATIONS.append(Surface.ROTATION_270, 180);
    }
    private String cameraId;
    protected CameraDevice cameraDevice;
    protected CameraCaptureSession cameraCaptureSessions;
    protected CaptureRequest captureRequest;
    protected CaptureRequest.Builder captureRequestBuilder;
    protected Size imageDimension;
    private ImageReader imageReader;
    private File file;
    private static final int REQUEST_CAMERA_PERMISSION = 200;
    private boolean mFlashSupported;
    private Handler mBackgroundHandler;
    private HandlerThread mBackgroundThread;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_full_screen);
        textureView = (TextureView) findViewById(R.id.texture_revcam);
        assert textureView != null;
        textureView.setSurfaceTextureListener(textureListener);
    }


    protected TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            //create a matrix to invert the x-plane, i.e undo the mirror
            Matrix matrix = new Matrix();
            matrix.setScale(-1, 1);
            //move it back to in view otherwise it'll be off to the left.
            matrix.postTranslate(width, 0);
            textureView.setTransform(matrix);
            //open your camera here
            openCamera();
        }
        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
            // Transform you image captured size according to the surface width and height
        }
        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            return false;
        }
        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        }
    };
    private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
        @Override
        public void onOpened(CameraDevice camera) {
            //This is called when the camera is open
            Log.e(TAG, "onOpened");
            cameraDevice = camera;
            createCameraPreview();
        }
        @Override
        public void onDisconnected(CameraDevice camera) {
            cameraDevice.close();
        }
        @Override
        public void onError(CameraDevice camera, int error) {
            cameraDevice.close();
            cameraDevice = null;
        }
    };
    final CameraCaptureSession.CaptureCallback captureCallbackListener = new CameraCaptureSession.CaptureCallback() {
        @Override
        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
            super.onCaptureCompleted(session, request, result);
            Toast.makeText(CameraApi.this, "Saved:" + file, Toast.LENGTH_SHORT).show();
            createCameraPreview();
        }
    };
    protected void startBackgroundThread() {
        mBackgroundThread = new HandlerThread("Camera Background");
        mBackgroundThread.start();
        mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
    }
    protected void stopBackgroundThread() {
        mBackgroundThread.quitSafely();
        try {
            mBackgroundThread.join();
            mBackgroundThread = null;
            mBackgroundHandler = null;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    protected void createCameraPreview() {
        try {
            SurfaceTexture texture = textureView.getSurfaceTexture();
            assert texture != null;
            texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
            Surface surface = new Surface(texture);
            captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            captureRequestBuilder.addTarget(surface);
            cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback(){
                @Override
                public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
                    //The camera is already closed
                    if (null == cameraDevice) {
                        return;
                    }
                    // When the session is ready, we start displaying the preview.
                    cameraCaptureSessions = cameraCaptureSession;
                    updatePreview();
                }
                @Override
                public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
                    Toast.makeText(CameraApi.this, "Configuration change", Toast.LENGTH_SHORT).show();
                }
            }, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
    private void openCamera() {
        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        Log.e(TAG, "is camera open");
        try {
            cameraId = manager.getCameraIdList()[0];
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
            StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            assert map != null;
            imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];
            // Add permission for camera and let user grant the permission
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(CameraApi.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
                return;
            }
            manager.openCamera(cameraId, stateCallback, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
        Log.e(TAG, "openCamera X");
    }
    protected void updatePreview() {
        if(null == cameraDevice) {
            Log.e(TAG, "updatePreview error, return");
        }
        captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
        try {
            cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
    private void closeCamera() {
        if (null != cameraDevice) {
            cameraDevice.close();
            cameraDevice = null;
        }
        if (null != imageReader) {
            imageReader.close();
            imageReader = null;
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CAMERA_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                // close the app
                Toast.makeText(CameraApi.this, "Sorry!!!, you can't use this app without granting permission", Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume");
        startBackgroundThread();
        if (textureView.isAvailable()) {
            openCamera();
        } else {
            textureView.setSurfaceTextureListener(textureListener);
        }
    }
    @Override
    protected void onPause() {
        Log.e(TAG, "onPause");
        //closeCamera();
        stopBackgroundThread();
        super.onPause();
    }


    public void hideSystemUI() {
        //for new api versions.
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(8);
    }

    public void checkCameraPermissions() {

        final boolean cameraGranted = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
        final boolean audioGranted = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
        final boolean audioNeeded = true;

        String[] perms = null;
        if (cameraGranted) {
            if (audioNeeded && !audioGranted) {
                perms = new String[]{Manifest.permission.RECORD_AUDIO};
            }
        } else {
            if (audioNeeded && !audioGranted) {
                perms = new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO};
            } else {
                perms = new String[]{Manifest.permission.CAMERA};
            }
        }

        if (perms != null) {
            ActivityCompat.requestPermissions(this, perms, 69);
        }
    }


}

I just want to switch to fullscreen when clicking a button, but it crashes the app. I've tried something like this:

cpHover.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder loadingFullscreen = new Builder(IdleModeFragment.this.getContext());
                loadingFullscreen.setMessage(getString(R.string.load_fullscreen));
                AlertDialog dialog = loadingFullscreen.show();

                Intent intent = new Intent(getContext(),CameraApi.class);
                startActivity(intent);
            }
        });

I don't know if there's another way, in principle I would just like to create a new instance of the camera (I suppose) and then switch to another tab (meaning another window) that only has the container of that preview.

Thanks in advance!

EDIT: Log is

03-09 11:41:46.936 8432-8432/com.microlay.nanodlp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.microlay.nanodlp, PID: 8432
                                                                    android.content.ActivityNotFoundException: Unable to find explicit activity class {com.microlay.nanodlp/com.microlay.nanodlp.utils.CameraApi}; have you declared this activity in your AndroidManifest.xml?
                                                                        at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
                                                                        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
                                                                        at android.app.Activity.startActivityForResult(Activity.java:3745)
                                                                        at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                                        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                                        at android.support.v4.app.ActivityCompatJB.startActivityForResult(ActivityCompatJB.java:30)
                                                                        at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:146)
                                                                        at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:932)
                                                                        at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:1047)
                                                                        at android.support.v4.app.Fragment.startActivity(Fragment.java:940)
                                                                        at android.support.v4.app.Fragment.startActivity(Fragment.java:929)
                                                                        at com.microlay.nanodlp.fragments.IdleModeFragment$2.onClick(IdleModeFragment.java:168)
                                                                        at android.view.View.performClick(View.java:4780)
                                                                        at android.view.View$PerformClick.run(View.java:19866)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5257)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)

EDIT 2: Black screen throws this error

03-09 13:10:52.838 28985-28985/com.microlay.nanodlp E/CameraAPI: onResume
03-09 13:10:52.852 28985-28985/com.microlay.nanodlp W/TextureView: A TextureView or a subclass can only be used with hardware acceleration enabled.
03-09 13:10:53.269 28985-28985/com.microlay.nanodlp E/WindowManager: android.view.WindowLeaked: Activity com.microlay.nanodlp.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{24cf85c6 V.E..... R.....I. 0,0-520,77} that was originally added here
                                                                         at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
                                                                         at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
                                                                         at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
                                                                         at android.app.Dialog.show(Dialog.java:298)
                                                                         at android.app.AlertDialog$Builder.show(AlertDialog.java:993)
                                                                         at com.microlay.nanodlp.fragments.IdleModeFragment$2.onClick(IdleModeFragment.java:166)
                                                                         at android.view.View.performClick(View.java:4780)
                                                                         at android.view.View$PerformClick.run(View.java:19866)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:135)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5257)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
Jorge
  • 27
  • 7
  • how does it crash the app? please post the error message. also you shouldn't open a dialog, then start a new activity. dismiss all your dialogs first – O.O.Balance Mar 09 '18 at 09:11
  • I added to the manifest like it recommends, and now it doesn't crash. Nonetheless it doesn't open the camera, the screen goes black. Maybe I'm not instatiating it correctly? – Jorge Mar 09 '18 at 10:50
  • Please follow this recommendation: https://stackoverflow.com/a/33285720/192373 – Alex Cohn Mar 09 '18 at 21:13
  • 1
    Thank you very much, @AlexCohn . This solution worked perfectly! – Jorge Mar 12 '18 at 08:35

2 Answers2

0

As written in error log you haven't declared your activity in your manifest. Add this line in your manifest.xml

<activity android:name="utils.CameraApi" />
Mukki
  • 1
0

As I have explained in my comment, you should not display a dialog, then start another Activity. That is what the WindowLeaked message is all about. To avoid it, you should dismiss any dialogs that are open – or not open this one at all:

AlertDialog.Builder loadingFullscreen = new Builder(IdleModeFragment.this.getContext());
                loadingFullscreen.setMessage(getString(R.string.load_fullscreen));
AlertDialog dialog = loadingFullscreen.show();

If you really want to show a dialog, then launch the other Activity, you could do it like this:

Intent intent = new Intent(getContext(), CameraApi.class);
new AlertDialog.Builder(this)
               .setMessage(getString(R.string.load_fullscreen))
               .setPositiveButton("OK", (dialog, which) -> startActivity(intent);)
               .setNegativeButton(null, null).create().show();

Once the user dismisses the dialog, the other Activity will launch. Thus no window will be leaked.

O.O.Balance
  • 2,930
  • 5
  • 23
  • 35