package org.opencv.samples.tutorial3;

import java.io.FileOutputStream;
import java.util.List;

import org.opencv.android.JavaCameraView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.util.AttributeSet;
import android.util.Log;

public class Tutorial3View extends JavaCameraView {

    private static final String TAG = "Sample::Tutorial3View";

    public Tutorial3View(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public List<String> getEffectList() {
        return mCamera.getParameters().getSupportedColorEffects();
    }

    public boolean isEffectSupported() {
        return (mCamera.getParameters().getColorEffect() != null);
    }

    public String getEffect() {
        return mCamera.getParameters().getColorEffect();
    }

    public void setEffect(String effect) {
        Camera.Parameters params = mCamera.getParameters();
        params.setColorEffect(effect);
        mCamera.setParameters(params);
    }

    public List<Size> getResolutionList() {
        return mCamera.getParameters().getSupportedPreviewSizes();
    }

    public void setResolution(Size resolution) {
        disconnectCamera();
        mMaxHeight = resolution.height;
        mMaxWidth = resolution.width;
        connectCamera(getWidth(), getHeight());
    }

    public Size getResolution() {
        return mCamera.getParameters().getPreviewSize();
    }

    public void takePicture(final String fileName) {
        Log.i(TAG, "Tacking picture");
        PictureCallback callback = new PictureCallback() {

            private String mPictureFileName = fileName;

            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                Log.i(TAG, "Saving a bitmap to file");
                Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
                try {
                    FileOutputStream out = new FileOutputStream(mPictureFileName);
                    picture.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    picture.recycle();
                    mCamera.startPreview();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        mCamera.takePicture(null, null, callback);
    }
}

@@ -168,79 +168,8 @@
 xt;%0A
-import android.graphics.Bitmap;%0Aimport android.graphics.BitmapFactory;%0A
 impo
@@ -391,16 +391,43 @@
 eraView 
+implements PictureCallback 
 %7B%0A%0A    p
@@ -483,16 +483,53 @@
 l3View%22;
+%0A    private String mPictureFileName;
 %0A%0A    pu
@@ -1640,17 +1640,16 @@
 TAG, %22Ta
-c
 king pic
@@ -1668,129 +1668,271 @@
     
+this.m
 Picture
-Callback callback = new PictureCallback() %7B%0A%0A            private String mPictureFileName = fileName;%0A
+FileName = fileName;%0A        // Call to garbage collector to avoid bug http://code.opencv.org/issues/2961 %0A        System.gc();%0A%0A        // PictureCallback is implemented by the current class%0A        mCamera.takePicture(null, null, this);
 %0A    
-  
+%7D%0A%0A
     
-  
 @Ove
@@ -1937,24 +1937,16 @@
 verride%0A
-        
     publ
@@ -2006,24 +2006,16 @@
         
-        
 Log.i(TA
@@ -2057,94 +2057,114 @@
     
-        Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length
+// The camera preview was automatically stopped. Start it again.%0A        mCamera.startPreview(
 );%0A
+%0A
         
     
@@ -2163,21 +2163,53 @@
     
-        try %7B
+// Write the image in a file (in jpeg format)
 %0A   
@@ -2205,32 +2205,38 @@
 format)%0A        
+try %7B%0A
             File
@@ -2248,19 +2248,19 @@
 tStream 
-out
+fos
  = new F
@@ -2294,24 +2294,17 @@
 eName);%0A
-        
+%0A
         
@@ -2311,154 +2311,51 @@
     
-picture.compress(Bitmap.CompressFormat.JPEG, 90, out);%0A                    picture.recycle();%0A                    mCamera.startPreview();%0A        
+fos.write(data);%0A            fos.close();%0A%0A
     
@@ -2367,16 +2367,26 @@
  catch (
+java.io.IO
 Exceptio
@@ -2408,136 +2408,78 @@
     
-        e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D;%0A%0A        mCamera.takePicture(null, null, callback);
+Log.e(%22PictureDemo%22, %22Exception in photoCallback%22, e);%0A        %7D%0A
 %0A    %7D%0A%7D
 %0A
@@ -2477,9 +2477,8 @@
 %0A    %7D%0A%7D
-%0A

Fix android tutorial 3 second picture taken bug