Exemplo n.º 1
2
  public void setCameraDisplaySetting() {

    // //Set Orentation
    if (!devicearraydata[2].equals("BYPASS")) { // --> For Camera Orentation Support
      cam.setDisplayOrientation(Integer.parseInt(devicearraydata[2]));
    }
    // //Set Camera Preview
    // Parameters param = cam.getParameters();
    // param.setPreviewSize(Integer.parseInt(devicearraydata[4]),
    // Integer.parseInt(devicearraydata[3]));
    try {
      // cam.setParameters(param);
      setPictureSizeOfScreenAndPreviewsize();
    } catch (Exception e) {
      PrintLog.print(activity, "Can't setPreviewSize");
      Parameters param = cam.getParameters();
      param.setPreviewSize(480, 640);
      cam.setParameters(param);
      PrintLog.print(activity, "set lowest PreviewSize 480x640");
    }
    PrintLog.print(activity.getApplicationContext(), Deviceinfo.BUILD_MODEL);
    PrintLog.print(
        activity.getApplicationContext(), Deviceinfo.getDeviceScreenOrentation(activity));
    PrintLog.print(
        activity.getApplicationContext(),
        "W"
            + Deviceinfo.getDeviceScreenSize(activity)[0]
            + "|H"
            + Deviceinfo.getDeviceScreenSize(activity)[1]
            + "|A"
            + Deviceinfo.getStatusBarHeight(activity));
  }
Exemplo n.º 2
0
  // Called when holder has changed
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

    Camera.Parameters parameters = camera.getParameters();
    List<String> focusModes = parameters.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
      Log.d("camera", "focus_mode_auto");
      parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    } else {
      Log.d("camera", "focus_mode_macro");
      parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
    }
    Log.d("camera", "orientation: " + getResources().getConfiguration().orientation);
    int orientation = getResources().getConfiguration().orientation;
    switch (orientation) {
      case Surface.ROTATION_0:
        Log.d("camera", "ROTATION_0");
        camera.setDisplayOrientation(180);
        break;
      case Surface.ROTATION_90:
        Log.d("camera", "ROTATION_90");
        camera.setDisplayOrientation(90);
        break;
      case Surface.ROTATION_180:
        Log.d("camera", "ROTATION_180");
        camera.setDisplayOrientation(0);
        break;
      case Surface.ROTATION_270:
        Log.d("camera", "ROTATION_270");
        camera.setDisplayOrientation(270);
        break;
    }

    camera.setParameters(parameters);
    camera.startPreview();
  }
  public void startCamera() {
    mCamera = Camera.open();
    try {
      Camera.Parameters parameters = mCamera.getParameters();
      Display display =
          ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

      if (display.getRotation() == Surface.ROTATION_0) {
        // parameters.setPreviewSize(height, width);
        mCamera.setDisplayOrientation(90);
      }

      if (display.getRotation() == Surface.ROTATION_90) {
        // parameters.setPreviewSize(width, height);
      }

      if (display.getRotation() == Surface.ROTATION_180) {
        // parameters.setPreviewSize(height, width);
      }

      if (display.getRotation() == Surface.ROTATION_270) {
        // parameters.setPreviewSize(width, height);
        mCamera.setDisplayOrientation(180);
      }
      mCamera.setParameters(parameters);
      mCamera.setPreviewDisplay(surface_holder);
      mCamera.startPreview();
      cameraRunning = true;
    } catch (IOException exception) {
      mCamera.release();
      mCamera = null;
    }
  }
Exemplo n.º 4
0
 /** **************************************************************** about Camera */
 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
   Log.d(TAG, "---->>>>>>>>>> surfaceChanged()");
   if (holder.getSurface() == null) {
     Log.d(TAG, "---- surfaceChanged(),  holder.getSurface() == null");
     return;
   }
   mSurfaceHolder = holder;
   try {
     hasCamera = true;
     nocamera.setVisibility(View.GONE);
     mCameraDevice = Camera.open(mCurrentCameraId);
   } catch (Exception e) {
     hasCamera = false;
     nocamera.setVisibility(View.VISIBLE);
     mSwitchBut.setVisibility(View.GONE);
     Log.e(TAG, " ____________- camera error");
     return;
   }
   if (mHardwareRotation == 0) // XXX
   mCameraDevice.setDisplayOrientation(90);
   if (mCurrentCameraId == 1) mCameraDevice.setDisplayOrientation(270); // XXX
   try {
     mCameraDevice.setPreviewDisplay(mSurfaceHolder);
   } catch (IOException e) {
     e.printStackTrace();
   }
   mCameraDevice.startPreview();
 }
Exemplo n.º 5
0
  /** 开启相机拍摄 */
  public void startCapture() {
    try {
      cameraInfo = new CameraInfo();
      if (mCamera == null) {
        // mCamera = Camera.open();
        camera_count = Camera.getNumberOfCameras();
        Log.e(TAG, "camera count:" + camera_count);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
          for (int i = 0; i < camera_count; i++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            // find front camera
            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
              Log.e(TAG, "to open front camera");
              mCamera = Camera.open(i);
              Camera.getCameraInfo(i, cameraInfo);
            }
          }
        }
        if (mCamera == null) {
          Log.e(TAG, "AAAAA OPEN camera");
          mCamera = Camera.open();
          Camera.getCameraInfo(0, cameraInfo);
        }
      }

      mCamera.stopPreview();
      mParameters = mCamera.getParameters();
      if (isScreenOriatationPortrait()) {
        if (cameraInfo.orientation == 270 || cameraInfo.orientation == 0)
          mCamera.setDisplayOrientation(90);
        if (cameraInfo.orientation == 90) mCamera.setDisplayOrientation(270);
      } else {
        if (cameraInfo.orientation == 90) mCamera.setDisplayOrientation(180);
      }

      mParameters.setPreviewSize(mwidth, mheight);
      mParameters.setPreviewFrameRate(15);
      mCamera.setParameters(mParameters);
      int mformat = mParameters.getPreviewFormat();
      int bitsperpixel = ImageFormat.getBitsPerPixel(mformat);
      Log.e(TAG, "pzy bitsperpixel: " + bitsperpixel);
      yuv_frame = new byte[mwidth * mheight * bitsperpixel / 8];
      yuv_Rotate90 = new byte[mwidth * mheight * bitsperpixel / 8];
      //            yuv_Rotate90lr = new byte[mwidth * mheight * bitsperpixel / 8];
      mCamera.addCallbackBuffer(yuv_frame);
      // mCamera.setPreviewDisplay(holder);
      mCamera.setPreviewDisplay(localSurfaceHolder);
      mCamera.setPreviewCallbackWithBuffer(this);

      EMVideoCallHelper.getInstance().setResolution(mwidth, mheight);

      mCamera.startPreview();
      Log.d(TAG, "camera start preview");
    } catch (Exception e) {
      e.printStackTrace();
      if (mCamera != null) mCamera.release();
    }
  }
 @Override
 public void onConfigurationChanged(Configuration newConfig) {
   super.onConfigurationChanged(newConfig);
   if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
     mCamera.setDisplayOrientation(90);
   } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
     mCamera.setDisplayOrientation(0);
   }
 }
Exemplo n.º 7
0
  private boolean prepareMediaRecorder() {
    myCamera = getCameraInstance(switchCam);
    Camera.Parameters parameters = myCamera.getParameters();
    parameters.setFlashMode(getFlashModeSetting());
    if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
      myCamera.setDisplayOrientation(90);
    } else {
      myCamera.setDisplayOrientation(0);
    }

    myCamera.setParameters(parameters);
    myCamera.unlock();

    mediaRecorder = new MediaRecorder();
    mediaRecorder.setCamera(myCamera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    if (switchCam == 1) {
      mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
      mediaRecorder.setVideoFrameRate(15);
    } else {
      mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
      mediaRecorder.setVideoFrameRate(30);
    }

    if (switchCam == 0) {
      if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        mediaRecorder.setOrientationHint(90);
      } else {
        mediaRecorder.setOrientationHint(0);
      }
    } else {
      mediaRecorder.setOrientationHint(270);
    }

    mediaRecorder.setOutputFile("/sdcard/" + customVideoPath + ".mp4");
    mediaRecorder.setMaxDuration(240000); // Set max duration 4 mins.
    mediaRecorder.setMaxFileSize(8000000); // Set max file size 8M
    mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());

    try {
      mediaRecorder.prepare();
    } catch (IllegalStateException e) {
      releaseMediaRecorder();
      return false;
    } catch (IOException e) {
      releaseMediaRecorder();
      return false;
    }
    return true;
  }
Exemplo n.º 8
0
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    try {
      // open the camera
      camera = Camera.open(1);
    } catch (RuntimeException e) {
      // check for exceptions
      System.err.println(e);
      return;
    }
    Camera.Parameters param;
    param = camera.getParameters();

    // modify parameter
    param.setPreviewSize(352, 288);
    camera.setParameters(param);
    try {
      // The Surface has been created, now tell the camera where to draw
      // the preview.
      camera.setPreviewDisplay(surfaceHolder);
      camera.setDisplayOrientation(90);
      camera.startPreview();
    } catch (Exception e) {
      // check for exceptions
      System.err.println(e);
      return;
    }
  }
Exemplo n.º 9
0
  protected synchronized void updateCamera() throws RuntimeException {

    // The camera is already correctly configured
    if (mUpdated) return;

    if (mPreviewStarted) {
      mPreviewStarted = false;
      mCamera.stopPreview();
    }

    Parameters parameters = mCamera.getParameters();
    mQuality = VideoQuality.determineClosestSupportedResolution(parameters, mQuality);
    int[] max = VideoQuality.determineMaximumSupportedFramerate(parameters);

    double ratio = (double) mQuality.resX / (double) mQuality.resY;
    mSurfaceView.requestAspectRatio(ratio);

    parameters.setPreviewFormat(mCameraImageFormat);
    parameters.setPreviewSize(mQuality.resX, mQuality.resY);
    parameters.setPreviewFpsRange(max[0], max[1]);

    try {
      mCamera.setParameters(parameters);
      mCamera.setDisplayOrientation(mOrientation);
      mCamera.startPreview();
      mPreviewStarted = true;
      mUpdated = true;
    } catch (RuntimeException e) {
      destroyCamera();
      throw e;
    }
  }
Exemplo n.º 10
0
  public void surfaceChanged(SurfaceHolder mholder, int format, int w, int h) {
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.

    if (mHolder.getSurface() == null) {
      // preview surface does not exist
      return;
    }

    // stop preview before making changes
    try {
      mCamera.stopPreview();
    } catch (Exception e) {
      Log.d(LOG_TAG, "Unable to stop preview");
    }
    Parameters p = mCamera.getParameters();
    mCamera.setDisplayOrientation(90);
    mCamera.setParameters(p);
    Log.d(LOG_TAG, "Surface changed");

    // set preview size and make any resize, rotate or
    // reformatting changes here

    // start preview with new settings
    try {
      mCamera.setPreviewDisplay(mholder);
      mCamera.startPreview();
      Log.d(LOG_TAG, "Starting camera preview ");
    } catch (IOException e) {
      Log.d(LOG_TAG, "Error starting camera preview " + e.getMessage());
    } catch (Exception e) {
      Log.d(LOG_TAG, "Error 2 starting camera preview " + e.getMessage());
    }
  }
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.

    if (mHolder.getSurface() == null) {
      // preview surface does not exist
      return;
    }

    // stop preview before making changes
    try {
      mCamera.stopPreview();
    } catch (Exception e) {
      // ignore: tried to stop a non-existent preview
    }

    // set preview size and make any resize, rotate or
    // reformatting changes here

    // start preview with new settings
    try {
      mCamera.setDisplayOrientation(90);
      mCamera.setPreviewDisplay(mHolder);
      mCamera.startPreview();
      startFaceDetection(); // start face detection feature
    } catch (Exception e) {
      Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
  }
Exemplo n.º 12
0
  @Override
  protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_take_photo);

    if (checkCameraHardware(getApplicationContext())) {
      camera = Camera.open();
      if (camera == null) {
        onBackPressed();
        Toast.makeText(this, R.string.camera_is_not_available, Toast.LENGTH_LONG).show();
      }

      camera.setDisplayOrientation(PORTRAIT_DISPLAY_ORIENTATION);

      final CameraPreview cameraPreview = new CameraPreview(this, camera);
      final FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
      preview.addView(cameraPreview);

      final Button takePhotoButton = (Button) findViewById(R.id.takePhotoOnCameraButton);
      final CameraPictureCallback pictureCallback = new CameraPictureCallback(this);
      takePhotoButton.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
              camera.takePicture(null, null, pictureCallback);
            }
          });
    } else {
      onBackPressed();
      Toast.makeText(this, R.string.no_camera_detected, Toast.LENGTH_LONG).show();
    }
  }
Exemplo n.º 13
0
  @Override
  public void alignCameraAndDisplayOrientation(WindowManager windowManager) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(id, info);
    int rotation = windowManager.getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
      case Surface.ROTATION_0:
        degrees = 0;
        break;
      case Surface.ROTATION_90:
        degrees = 90;
        break;
      case Surface.ROTATION_180:
        degrees = 180;
        break;
      case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
      result = (info.orientation + degrees) % 360;
      result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
      result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
  }
Exemplo n.º 14
0
  void setCameraDisplayOrientation(int cameraId) {
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
      case Surface.ROTATION_0:
        degrees = 0;
        break;
      case Surface.ROTATION_90:
        degrees = 90;
        break;
      case Surface.ROTATION_180:
        degrees = 180;
        break;
      case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result = 0;

    CameraInfo info = new CameraInfo();
    Camera.getCameraInfo(cameraId, info);

    if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
      result = ((360 - degrees) + info.orientation);
    } else if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
      result = ((360 - degrees) - info.orientation);
      result += 360;
    }
    result = result % 360;
    camera.setDisplayOrientation(result);
  }
  public void setCameraDisplayOrientation() {
    if (camera == null || cameraInfo == null) {
      return;
    }

    WindowManager winManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    int rotation = winManager.getDefaultDisplay().getRotation();

    int degrees = 0;

    switch (rotation) {
      case Surface.ROTATION_0:
        degrees = 0;
        break;
      case Surface.ROTATION_90:
        degrees = 90;
        break;
      case Surface.ROTATION_180:
        degrees = 180;
        break;
      case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int mRotation;

    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
      mRotation = (cameraInfo.orientation + degrees) % 360;
      mRotation = (360 - mRotation) % 360; // compensate the mirror
    } else { // back-facing
      mRotation = (cameraInfo.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(mRotation);
  }
Exemplo n.º 16
0
 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
   // If your preview can change or rotate, take care of those events here.
   // Make sure to stop the preview before resizing or reformatting it.
   // Change the weird default display orientation.
   mCamera.setDisplayOrientation(90);
   refreshCamera(mCamera);
 }
Exemplo n.º 17
0
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    Log.e(TAG, "surfaceChanged => w=" + w + ", h=" + h);
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.
    if (mHolder.getSurface() == null) {
      // preview surface does not exist
      return;
    }

    // stop preview before making changes
    try {
      mCamera.stopPreview();
    } catch (Exception e) {
      // ignore: tried to stop a non-existent preview
    }

    // set preview size and make any resize, rotate or reformatting changes here
    // start preview with new settings
    try {
      Camera.Parameters parameters = mCamera.getParameters();

      /* INSERT ZOOM HERE */

      parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
      parameters.setRotation(90);
      mCamera.setParameters(parameters);
      mCamera.setDisplayOrientation(90);

      mCamera.setPreviewDisplay(mHolder);
      mCamera.startPreview();

    } catch (Exception e) {
      Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
  }
Exemplo n.º 18
0
 /** Asks the camera hardware to begin drawing preview frames to the screen. */
 public void startPreview() {
   if (camera != null && !previewing) {
     camera.setDisplayOrientation(90);
     camera.startPreview();
     previewing = true;
   }
 }
Exemplo n.º 19
0
 public void updateDisplayOrientation(Context context) {
   if (!opened) throw new IllegalStateException("Camera not opened yet");
   joinPreviewStarter();
   final boolean restartPreview =
       previewing && Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
   if (restartPreview) {
     previewing = false;
     camera.stopPreview();
   }
   int rotation =
       ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
           .getDefaultDisplay()
           .getRotation();
   int degrees = 0;
   switch (rotation) {
     case Surface.ROTATION_0:
       degrees = 0;
       break;
     case Surface.ROTATION_90:
       degrees = 90;
       break;
     case Surface.ROTATION_180:
       degrees = 180;
       break;
     case Surface.ROTATION_270:
       degrees = 270;
       break;
   }
   cameraRotation = (cameraOrientation - degrees + 360) % 360;
   camera.setDisplayOrientation(cameraRotation);
   if (restartPreview) {
     startPreview();
   }
 }
  @SuppressWarnings("WrongConstant")
  private void setCameraDisplayOrientation(Camera.Parameters parameters) {
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(getCurrentCameraId(), info);
    final int deviceOrientation = Degrees.getDisplayRotation(getActivity());
    mDisplayOrientation =
        Degrees.getDisplayOrientation(
            info.orientation,
            deviceOrientation,
            info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT);
    Log.d(
        "CameraFragment",
        String.format(
            "Orientations: Sensor = %d˚, Device = %d˚, Display = %d˚",
            info.orientation, deviceOrientation, mDisplayOrientation));

    int previewOrientation;
    if (CameraUtil.isArcWelder()) {
      previewOrientation = 0;
    } else {
      previewOrientation = mDisplayOrientation;
      if (Degrees.isPortrait(deviceOrientation)
          && getCurrentCameraPosition() == CAMERA_POSITION_FRONT)
        previewOrientation = Degrees.mirror(mDisplayOrientation);
    }
    parameters.setRotation(previewOrientation);
    mCamera.setDisplayOrientation(previewOrientation);
  }
Exemplo n.º 21
0
  public static void setCameraDisplayOrientation(
      Activity activity, int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
      case Surface.ROTATION_0:
        degrees = 0;
        break;
      case Surface.ROTATION_90:
        degrees = 90;
        break;
      case Surface.ROTATION_180:
        degrees = 180;
        break;
      case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
      result = (info.orientation + degrees) % 360;
      result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
      result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
  }
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    /*
     * If your preview can change or rotate, take care of those events here.
     * Make sure to stop the preview before resizing or reformatting it.
     */
    if (mHolder.getSurface() == null) {
      // preview surface does not exist
      return;
    }

    // stop preview before making changes
    try {
      mCamera.stopPreview();
    } catch (Exception e) {
      // ignore: tried to stop a non-existent preview
    }

    try {
      // Hard code camera surface rotation 90 degs to match Activity view in portrait
      mCamera.setDisplayOrientation(90);

      mCamera.setPreviewDisplay(mHolder);
      mCamera.setPreviewCallback(previewCallback);
      mCamera.startPreview();
      mCamera.autoFocus(autoFocusCallback);
    } catch (Exception e) {
      Log.d("DBG", "Error starting camera preview: " + e.getMessage());
    }
  }
Exemplo n.º 23
0
  /** sets the Orientation of the camera Preview o be the same as MoSyncApp */
  private void setCameraDisplayOrientation() {
    // Use reflection to correct the preview on 2.2 and higher versions
    try {
      mCameraDisplayOrientation =
          mCamera.getClass().getMethod("setDisplayOrientation", Integer.TYPE);

      // Set the orientation of the picture on old Android phones
      Camera.Parameters parameters = mCamera.getParameters();

      if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        parameters.set("orientation", "portrait");
        // default camera orientation on android is landscape
        // So we need to rotate the preview
        parameters.setRotation(90);
        mCamera.setDisplayOrientation(90);
      } else if (getResources().getConfiguration().orientation
          == Configuration.ORIENTATION_LANDSCAPE) {
        parameters.set("orientation", "landscape");
        parameters.setRotation(0);
        mCamera.setDisplayOrientation(0);
      }
      mCamera.setParameters(parameters);
    } catch (NoSuchMethodException nsme) {
      SYSLOG("ANDROID Version is less than 2.2!!");
      // Set the orientation of the picture on old Android phones
      Camera.Parameters parameters = mCamera.getParameters();

      // an Ugly hack to make HTC wildfire work
      if (Build.MANUFACTURER.equals("HTC")) {
        parameters.set("orientation", "landscape");
        parameters.setRotation(90);
      }
      // rest of the phones work fine with the standard settings
      else if (getResources().getConfiguration().orientation
          == Configuration.ORIENTATION_PORTRAIT) {
        parameters.set("orientation", "portrait");
        parameters.setRotation(90);
      } else if (getResources().getConfiguration().orientation
          == Configuration.ORIENTATION_LANDSCAPE) {
        parameters.set("orientation", "landscape");
        parameters.setRotation(0);
      }
      mCamera.setParameters(parameters);
    } catch (RuntimeException e) {
      SYSLOG("Failed to set camera Parameters");
    }
  }
Exemplo n.º 24
0
  public CustomeCamera(
      Activity activity, FrameLayout cameraPreviewLayout, Integer size, Integer quality)
      throws Exception {
    this.activity = activity;
    this.size = size;
    this.quality = quality;

    // Create an instance of Camera
    try {
      mCamera = getCameraInstance();
    } catch (Exception e) {
      throw new Exception(e.getMessage());
    }
    try {
      mCamera.setDisplayOrientation(90);
    } catch (Exception e) {
      Log.d(TAG, e.getMessage());
    }

    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
      case Surface.ROTATION_0:
        degrees = 0;
        break; // Natural orientation
      case Surface.ROTATION_90:
        degrees = 90;
        break; // Landscape left
      case Surface.ROTATION_180:
        degrees = 180;
        break; // Upside down
      case Surface.ROTATION_270:
        degrees = 270;
        break; // Landscape right
    }
    int rotate = (info.orientation - degrees + 360) % 360;

    // STEP #2: Set the 'rotation' parameter
    Camera.Parameters params = mCamera.getParameters();
    params.setRotation(rotate);

    List<String> focusModes = params.getSupportedFocusModes();
    if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
      params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
    }
    if (params.getFocusMode() == Camera.Parameters.FOCUS_MODE_AUTO) autoFocusEnabled = true;
    else autoFocusEnabled = false;

    // TODO for the test
    autoFocusEnabled = false;

    mCamera.setParameters(params);

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(activity, mCamera);
    cameraPreviewLayout.addView(mPreview);
  }
Exemplo n.º 25
0
  protected synchronized void createCamera() throws RuntimeException {
    if (mSurfaceView == null) throw new InvalidSurfaceException("Invalid surface !");
    if (mSurfaceView.getHolder() == null || !mSurfaceReady)
      throw new InvalidSurfaceException("Invalid surface !");

    if (mCamera == null) {
      openCamera();
      mUpdated = false;
      mUnlocked = false;
      mCamera.setErrorCallback(
          new Camera.ErrorCallback() {
            @Override
            public void onError(int error, Camera camera) {
              // On some phones when trying to use the camera facing front the media server will die
              // Whether or not this callback may be called really depends on the phone
              if (error == Camera.CAMERA_ERROR_SERVER_DIED) {
                // In this case the application must release the camera and instantiate a new one
                Log.e(TAG, "Media server died !");
                // We don't know in what thread we are so stop needs to be synchronized
                mCameraOpenedManually = false;
                stop();
              } else {
                Log.e(TAG, "Error unknown with the camera: " + error);
              }
            }
          });

      try {

        // If the phone has a flash, we turn it on/off according to mFlashEnabled
        // setRecordingHint(true) is a very nice optimization if you plane to only use the Camera
        // for recording
        Parameters parameters = mCamera.getParameters();
        if (parameters.getFlashMode() != null) {
          parameters.setFlashMode(
              mFlashEnabled ? Parameters.FLASH_MODE_TORCH : Parameters.FLASH_MODE_OFF);
        }
        parameters.setRecordingHint(true);
        mCamera.setParameters(parameters);
        mCamera.setDisplayOrientation(mOrientation);

        try {
          if (mMode == MODE_MEDIACODEC_API_2) {
            mSurfaceView.startGLThread();
            mCamera.setPreviewTexture(mSurfaceView.getSurfaceTexture());
          } else {
            mCamera.setPreviewDisplay(mSurfaceView.getHolder());
          }
        } catch (IOException e) {
          throw new InvalidSurfaceException("Invalid surface !");
        }

      } catch (RuntimeException e) {
        destroyCamera();
        throw e;
      }
    }
  }
 public void previewCamera() {
   try {
     mCamera.setPreviewDisplay(mHolder);
     mCamera.setDisplayOrientation(90);
     mCamera.startPreview();
   } catch (Exception e) {
     Log.d(DEBUG_TAG, "Cannot start preview", e);
   }
 }
  void setDesiredCameraParameters(OpenCamera camera, boolean safeMode) {

    Camera theCamera = camera.getCamera();
    Camera.Parameters parameters = theCamera.getParameters();

    //    theCamera.setDisplayOrientation(90);//add 这句代码作用是旋转镜头90度,使相机预览方向正确显示

    if (parameters == null) {
      Log.w(
          TAG,
          "Device error: no camera parameters are available. Proceeding without configuration.");
      return;
    }

    Log.i(TAG, "Initial camera parameters: " + parameters.flatten());

    if (safeMode) {
      Log.w(TAG, "In camera config safe mode -- most settings will not be honored");
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

    initializeTorch(parameters, prefs, safeMode);

    CameraConfigurationUtils.setFocus(parameters, true, true, safeMode);

    if (!safeMode) {
      CameraConfigurationUtils.setBarcodeSceneMode(parameters);
      CameraConfigurationUtils.setVideoStabilization(parameters);
      CameraConfigurationUtils.setFocusArea(parameters);
      CameraConfigurationUtils.setMetering(parameters);
    }

    parameters.setPreviewSize(bestPreviewSize.x, bestPreviewSize.y);

    theCamera.setParameters(parameters);

    theCamera.setDisplayOrientation(cwRotationFromDisplayToCamera);

    Camera.Parameters afterParameters = theCamera.getParameters();
    Camera.Size afterSize = afterParameters.getPreviewSize();
    if (afterSize != null
        && (bestPreviewSize.x != afterSize.width || bestPreviewSize.y != afterSize.height)) {
      Log.w(
          TAG,
          "Camera said it supported preview size "
              + bestPreviewSize.x
              + 'x'
              + bestPreviewSize.y
              + ", but after setting it, preview size is "
              + afterSize.width
              + 'x'
              + afterSize.height);
      bestPreviewSize.x = afterSize.width;
      bestPreviewSize.y = afterSize.height;
    }
  }
Exemplo n.º 28
0
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private boolean prepareVideoRecorder() {

    // BEGIN_INCLUDE (configure_preview)
    mCamera = CameraHelper.getDefaultCameraInstance();

    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    mCamera.setDisplayOrientation(90);
    mCamera.setParameters(parameters);

    // BEGIN_INCLUDE (configure_media_recorder)
    mMediaRecorder = new MediaRecorder();

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();

    // Camera.Parameters parameters = mCamera.getParameters();

    mMediaRecorder.setCamera(mCamera);
    CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_QVGA);

    // Log.d(TAG, profile.videoBitRate+"<- videoBitRate r");
    profile.videoBitRate = 1024000;

    // Step 2: Set sources
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set all values contained in profile except audio settings
    mMediaRecorder.setOutputFormat(profile.fileFormat);
    mMediaRecorder.setVideoEncoder(profile.videoCodec);
    mMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
    mMediaRecorder.setVideoFrameRate(profile.videoFrameRate);
    mMediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
    mMediaRecorder.setOrientationHint(90);
    mMediaRecorder.setVideoFrameRate(24);

    // Step 4: Set output file
    save_to = CameraHelper.getOutputMediaFile(CameraHelper.MEDIA_TYPE_VIDEO).toString();
    mMediaRecorder.setOutputFile(save_to);

    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    // Step 5: Prepare configured MediaRecorder
    try {
      mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
      Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
      releaseMediaRecorder();
      return false;
    } catch (IOException e) {
      Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
      releaseMediaRecorder();
      return false;
    }
    return true;
  }
Exemplo n.º 29
0
 /**
  * Sets the camera up to take preview images which are used for both preview and decoding. We
  * detect the preview format here so that buildLuminanceSource() can build an appropriate
  * LuminanceSource subclass. In the future we may want to force YUV420SP as it's the smallest, and
  * the planar Y can be used for barcode scanning without a copy in some cases.
  */
 void setDesiredCameraParameters(Camera camera) {
   Camera.Parameters parameters = camera.getParameters();
   Log.d(TAG, "Setting preview size: " + cameraResolution);
   parameters.setPreviewSize(cameraResolution.x, cameraResolution.y);
   setFlash(parameters);
   setZoom(parameters);
   // setSharpness(parameters);
   camera.setDisplayOrientation(90);
   camera.setParameters(parameters);
 }
Exemplo n.º 30
0
 // 设置相机的预览
 private void setStartProview(Camera camera, SurfaceHolder holder) {
   try {
     camera.setPreviewDisplay(holder);
     // 旋转的 camera  的角度
     camera.setDisplayOrientation(90);
     camera.startPreview();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }