0

I am developing an android application in which the camera preview is shown at all times. When the user wants, he can take a picture of that preview with the takePicture (), and apply different filters that picture.

The problem is that when I take a picture with takePicture () method, the picture freezes for a while and I want this freeze-frame remains as long as the user wants to apply the necessary filters, and when he wants, return to preview mode the camera immediately.

I am using the OpenCV library and specifically the JavaCameraView class to get the parameters of the camera and then take the pictures I want.

mCamera.takePicture(null,null,new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera myCamera) {
                if(data != null){
                    bm = BitmapFactory.decodeByteArray(data, 0, data.length);
                    Log.i(TAG, "Caracteristicas de la foto: "+bm.getWidth()+"x"+bm.getHeight());
                    mCamera.startPreview();
                }
            }
        };

In the onPictureTaken () method I store the image into a bitmap, and then to draw it on a canvas with the resolution of the camera. This will be done in CameraBridgeViewBase class.

Thank you in advance!

ales
  • 33
  • 3
  • Please clarify: are you not satisfied with the time it takes for the camera to (re)start to display live preview after for some while the screen was filled with a captured picture (or its derivatives)? – Alex Cohn Oct 27 '14 at 23:27
  • Yes, the problem is the time that the program takes to go back to live preview after the user stops using the picture taken. And I want this time to return to the preview immediately. – ales Oct 28 '14 at 12:35
  • There is another problem with your `onPictureTaken()` method: the bitmap may be terribly large, causing out-of-memory exception. See [_Android onPictureTaken callback throws out of memory exception in Bitmap.decodeByteArray_](http://stackoverflow.com/questions/13725082/android-onpicturetaken-callback-throws-out-of-memory-exception-in-bitmap-decodeb) – Alex Cohn Oct 29 '14 at 17:22
  • This sometimes happens to me... I think that this is a problem of buffers and I should empty before calling takePicture() with: `mCamera.setPreviewCallback(null);`. But as you say, the bitmap is too large and I'm having problems to draw it on the canvas for this reason, because the canvas has the dimensions 960x540 and the bitmap has 3264x2448. What can I do to get well draw it on the canvas? – ales Oct 31 '14 at 00:31
  • You should read the linked Q and links around it. The easiest way to avoid OOM is to scale the bitmap down. – Alex Cohn Oct 31 '14 at 13:07
  • What is the linked Q?. Which you commented me before?. On the other hand, do you think I should scale the bitmap or resize the canvas?. Is that if I scale the bitmap down, the bitmap can lose resolution... and I need the best possible resolution of the camera because when you want to zoom in the canvas with this bitmap, you can see every detail perfectly. If you could answer me with lines of code I'd appreciate it. – ales Oct 31 '14 at 16:34

1 Answers1

0

You can restart preview immediately from onPictureTaken() callback. The trick is that you put the ImageView that displays the captured image on top of the SurfaceView where you display live preview. Now, it's enough to imageView.setVisibility(View.GONE) and your preview will be immediately live.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • If you are not using an ImageView for the bitmap, but draw on the same canvas instead, and you call `CameraBridgeViewBase.disableView()` - then you should probably consider adding an `ImageView` on top of the `CameraBridgeViewBase`-derived view, and switching its visibility instead. – Alex Cohn Oct 28 '14 at 17:55
  • I do not understand what you want to say me. Sorry, but I'm new at this. How should I implement onPictureToken() method then? Do you need to show you more code?. Thank you very much for everything – ales Oct 29 '14 at 14:59
  • Unfortunately, I have not got a working example for you right now; you need an additional view (ImageView). The only change to your onPictureTaken() would be to add a call to make this ImageView to display the bitmap and get it visible. – Alex Cohn Oct 29 '14 at 17:16
  • I added these three lines of code below the line `bm=BitmapFactory.decodeByteArray(data, 0, data.length);` :`ImageView imageView = new ImageView(getContext()); imageView.setImageBitmap(bm); imageView.setVisibility(View.VISIBLE);` and nothing happens. Can you tell me what lines of code I write exactly? Thank you very much – ales Oct 31 '14 at 00:20
  • No, you should define the ImageView in you layout.xml. – Alex Cohn Oct 31 '14 at 13:08