Example #1
0
  @Override
  public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) {
    Log.e("ok", "surfaceChanged => w=" + i2 + ", h=" + i3);
    // before changing the application orientation, you need to stop the preview, rotate and then
    // start it again
    if (surfaceHolder.getSurface() == null) // check if the surface is ready to receive camera data
    return;

    try {
      mCamera.stopPreview();
    } catch (Exception e) {
      // this will happen when you are trying the camera if it's not running
    }

    // now, recreate the camera preview
    try {

      Camera.Parameters parameters = mCamera.getParameters();
      List<String> focusModes = parameters.getSupportedFocusModes();
      if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
      }
      parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
      mCamera.setParameters(parameters);
      mCamera.setDisplayOrientation(90);
      mCamera.startPreview(mHolder);
    } catch (IOException e) {
      Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage());
    }
  }
Example #2
0
  /** Initialize camera with all good paramters. */
  private void initializeCamera() {
    // - Open camera.
    mEasyCamera = DefaultEasyCamera.open();

    // - Make sure preview is not started.
    mEasyCamera.stopPreview();

    // - Set good parameters.
    Camera.Parameters params = mEasyCamera.getParameters();
    params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
    mEasyCamera.setParameters(params);
    mEasyCamera.setDisplayOrientation(90);
  }
Example #3
0
  @SuppressWarnings("deprecation")
  public EasyCameraView(Context context, EasyCamera camera) {
    super(context);
    final Context context1 = getContext();
    mCamera = camera;
    mCamera.setDisplayOrientation(90);

    mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
    for (Camera.Size str : mSupportedPreviewSizes) Log.e(TAG, str.width + "/" + str.height);

    // get the holder and set this class as the callback, so we can get camera data here
    mHolder = getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    callback =
        new PictureCallback() {
          public void onPictureTaken(byte[] data, EasyCamera.CameraActions actions) {
            Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
            File pictureFileDir = getDir();
            if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

              Log.d("Log", "Can't create directory to save image.");
              return;
            }

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
            String date = dateFormat.format(new Date());
            String photoFile = "Picture_" + date + ".jpg";

            String filename = pictureFileDir.getPath() + File.separator + photoFile;

            File pictureFile = new File(filename);

            try {
              FileOutputStream fos = new FileOutputStream(pictureFile);
              fos.write(data);
              fos.close();
              Toast.makeText(context1, "New Image saved:" + filename, Toast.LENGTH_LONG).show();
              Log.d("Log", "New Image saved:" + filename);
            } catch (Exception error) {
              Log.d("Log", "File" + filename + "not saved: " + error.getMessage());
            }
          }
        };
  }
Example #4
0
 /**
  * Get camera preview's ratio.
  *
  * @return Preview's ratio.
  */
 private float getCameraPreviewRatio() {
   final Camera.Size size = mEasyCamera.getParameters().getPreviewSize();
   return (float) size.width / size.height;
 }