gt:     protected boolean connectCamera(int width, int height) {
context: package com.ajscape.pixatoon.ui.views;
  
  import java.util.List;
  
  import android.content.Context;
  import android.graphics.ImageFormat;
  import android.graphics.SurfaceTexture;
  import android.hardware.Camera;
  import android.hardware.Camera.PreviewCallback;
  import android.os.Build;
  import android.util.AttributeSet;
  import android.util.Log;
  import android.view.ViewGroup.LayoutParams;
  
  import org.opencv.android.CameraBridgeViewBase;
  import org.opencv.core.Core;
  import org.opencv.core.CvType;
  import org.opencv.core.Mat;
  import org.opencv.core.Size;
  import org.opencv.imgproc.Imgproc;
  
  /**
   * Portrait OpenCV Camera View, modified from OpenCV JavaCameraView, for rendering camera frames with filter effects
   */
  public class OpenCvCameraView extends CameraBridgeViewBase implements PreviewCallback, Camera.PictureCallback {
  
      private static final int MAGIC_TEXTURE_ID = 10;
      private static final String TAG = "CameraView";
  
      private byte mBuffer[];
      private Mat[] mFrameChain;
      private int mChainIdx = 0;
      private Thread mThread;
      private boolean mStopThread;
  
      protected Camera mCamera;
      protected JavaCameraFrame[] mCameraFrame;
      private SurfaceTexture mSurfaceTexture;
      private PictureCallback mPictureCallback;
  
      public static class JavaCameraSizeAccessor implements ListItemAccessor {
  
          public int getWidth(Object obj) {
              Camera.Size size = (Camera.Size) obj;
              return size.width;
          }
  
          public int getHeight(Object obj) {
              Camera.Size size = (Camera.Size) obj;
              return size.height;
          }
      }
  
      public interface PictureCallback {
          void onPictureTaken(byte[] data, int height, int width);
      }
  
      public OpenCvCameraView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }
  
      public void setPictureCallback(PictureCallback pictureCallback) { mPictureCallback = pictureCallback; }
  
      public boolean switchCamera() {
          if(Camera.getNumberOfCameras() > 1) {
              if(mCameraIndex != CAMERA_ID_FRONT )
                  mCameraIndex = CAMERA_ID_FRONT;
              else
                  mCameraIndex = CAMERA_ID_BACK;
              return true;
          } else {
              return false;
          }
      }
  
  
      protected boolean initializeCamera(int width, int height) {
          Log.d(TAG, "Initialize java camera");
          boolean result = true;
          synchronized (this) {
              mCamera = null;
  
              if (mCameraIndex == CAMERA_ID_ANY) {
                  Log.d(TAG, "Trying to open camera with old open()");
                  try {
                      mCamera = Camera.open();
                  }
                  catch (Exception e){
                      Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage());
                  }
  
                  if(mCamera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                      boolean connected = false;
                      for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
                          Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(camIdx) + ")");
                          try {
                              mCamera = Camera.open(camIdx);
                              connected = true;
                          } catch (RuntimeException e) {
                              Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage());
                          }
                          if (connected) break;
                      }
                  }
              } else {
                  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                      int localCameraIndex = mCameraIndex;
                      if (mCameraIndex == CAMERA_ID_BACK) {
                          Log.i(TAG, "Trying to open back camera");
                          Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
                          for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
                              Camera.getCameraInfo( camIdx, cameraInfo );
                              if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                                  localCameraIndex = camIdx;
                                  break;
                              }
                          }
                      } else if (mCameraIndex == CAMERA_ID_FRONT) {
                          Log.i(TAG, "Trying to open front camera");
                          Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
                          for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
                              Camera.getCameraInfo( camIdx, cameraInfo );
                              if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                                  localCameraIndex = camIdx;
                                  break;
                              }
                          }
                      }
                      if (localCameraIndex == CAMERA_ID_BACK) {
                          Log.e(TAG, "Back camera not found!");
                      } else if (localCameraIndex == CAMERA_ID_FRONT) {
                          Log.e(TAG, "Front camera not found!");
                      } else {
                          Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(localCameraIndex) + ")");
                          try {
                              mCamera = Camera.open(localCameraIndex);
                          } catch (RuntimeException e) {
                              Log.e(TAG, "Camera #" + localCameraIndex + "failed to open: " + e.getLocalizedMessage());
                          }
                      }
                  }
              }
  
              if (mCamera == null)
                  return false;
  
          /* Now set camera parameters */
              try {
                  Camera.Parameters params = mCamera.getParameters();
                  Log.d(TAG, "getSupportedPreviewSizes()");
                  List<Camera.Size> sizes = params.getSupportedPreviewSizes();
  
                  if (sizes != null) {
                  /* Select the size that fits surface considering maximum size allowed */
                      Size frameSize = calculateCameraFrameSize(sizes, new JavaCameraSizeAccessor(), height, width); //use turn around values here to get the correct prev size for portrait mode
  
                      params.setPreviewFormat(ImageFormat.NV21);
                      Log.d(TAG, "Set preview size to " + Integer.valueOf((int)frameSize.width) + "x" + Integer.valueOf((int)frameSize.height));
                      params.setPreviewSize((int)frameSize.width, (int)frameSize.height);
  
                      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH)
                          params.setRecordingHint(true);
  
                      List<String> FocusModes = params.getSupportedFocusModes();
                      if (FocusModes != null && FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
                      {
                          params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
                      }
  
                      mCamera.setParameters(params);
                      params = mCamera.getParameters();
  
                      mFrameWidth = params.getPreviewSize().height; //the frame width and height of the super class are used to generate the cached bitmap and they need to be the size of the resulting frame
                      mFrameHeight = params.getPreviewSize().width;
  
                      int realWidth = mFrameHeight; //the real width and height are the width and height of the frame received in onPreviewFrame
                      int realHeight = mFrameWidth;
  
                      if ((getLayoutParams().width == LayoutParams.MATCH_PARENT) && (getLayoutParams().height == LayoutParams.MATCH_PARENT))
                          mScale = Math.min(((float)height)/mFrameHeight, ((float)width)/mFrameWidth);
                      else
                          mScale = 0;
  
                      if (mFpsMeter != null) {
                          mFpsMeter.setResolution(mFrameWidth, mFrameHeight);
                      }
  
                      int size = mFrameWidth * mFrameHeight;
                      size  = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
                      mBuffer = new byte[size];
  
                      mCamera.addCallbackBuffer(mBuffer);
                      mCamera.setPreviewCallbackWithBuffer(this);
  
                      mFrameChain = new Mat[2];
                      mFrameChain[0] = new Mat(realHeight + (realHeight/2), realWidth, CvType.CV_8UC1); //the frame chane is still in landscape
                      mFrameChain[1] = new Mat(realHeight + (realHeight/2), realWidth, CvType.CV_8UC1);
  
                      AllocateCache();
  
                      mCameraFrame = new JavaCameraFrame[2];
                      mCameraFrame[0] = new JavaCameraFrame(mFrameChain[0], mFrameWidth, mFrameHeight); //the camera frame is in portrait
                      mCameraFrame[1] = new JavaCameraFrame(mFrameChain[1], mFrameWidth, mFrameHeight);
  
                      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                          mSurfaceTexture = new SurfaceTexture(MAGIC_TEXTURE_ID);
                          mCamera.setPreviewTexture(mSurfaceTexture);
                      } else
                          mCamera.setPreviewDisplay(null);
  
                  /* Finally we are ready to start the preview */
                      Log.d(TAG, "startPreview");
                      mCamera.startPreview();
                  }
                  else
                      result = false;
              } catch (Exception e) {
                  result = false;
                  e.printStackTrace();
              }
          }
          return result;
      }
  
      protected void releaseCamera() {
          synchronized (this) {
              if (mCamera != null) {
                  mCamera.stopPreview();
                  mCamera.setPreviewCallback(null);
  
                  mCamera.release();
              }
              mCamera = null;
              if (mFrameChain != null) {
                  mFrameChain[0].release();
                  mFrameChain[1].release();
              }
              if (mCameraFrame != null) {
                  mCameraFrame[0].release();
                  mCameraFrame[1].release();
              }
          }
      }
  
      @Override
  