/**
   * An equivalent to maCameraNumber syscall that queries the number of available cameras
   *
   * @return number of cameras on the device
   */
  public int numberOfCameras() {
    if (mNumCameras != 0) {
      // Do not do the costly operation of reflection again
      return mNumCameras;
    }
    try {
      if (mCamera == null) {
        tempCamera = Camera.open();
      } else {
        mCamera.release();
        tempCamera = Camera.open(mCurrentCameraIndex);
      }

      // We have to use and static instance of the camera in the reflection here
      mGetNumberofCameras = tempCamera.getClass().getMethod("getNumberOfCameras");
      tempCamera.release();
      if (mCamera != null) {
        mCamera = Camera.open(mCurrentCameraIndex);
      }
      return Camera.getNumberOfCameras();

    } catch (NoSuchMethodException nsme) {
      tempCamera.release();
      SYSLOG("ANDROID Version is less than 2.3!!");
      // before 2.3 only one camera is supported
      return 1;
    } catch (RuntimeException e) {
      SYSLOG("Failed to set camera Parameters");
      return MAAPI_consts.MA_CAMERA_RES_FAILED;
    }
  }
  /** Opens a rear-facing camera with {@link Camera#open(int)}, if one exists, or opens camera 0. */
  public static Camera open() {

    int numCameras = Camera.getNumberOfCameras();
    if (numCameras == 0) {
      Log.w(TAG, "No cameras!");
      return null;
    }

    int index = 0;
    while (index < numCameras) {
      Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
      Camera.getCameraInfo(index, cameraInfo);
      // CAMERA_FACING_BACK:手机背面的摄像头
      if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
        break;
      }
      index++;
    }

    Camera camera;
    if (index < numCameras) {
      Log.i(TAG, "Opening camera #" + index);
      camera = Camera.open(index);
    } else {
      Log.i(TAG, "No camera facing back; returning camera #0");
      camera = Camera.open(0);
    }

    return camera;
  }
Exemplo n.º 3
0
  @SuppressLint("NewApi")
  @Override
  public void surfaceCreated(SurfaceHolder holder) {

    int cameraIndex =
        Integer.parseInt(
            PreferenceManager.getDefaultSharedPreferences(getContext())
                .getString("pref_cameraIndex", "0"));
    Log.i(TAG, "Opening camera " + (cameraIndex + 1));
    try {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD)
        camera = Camera.open(cameraIndex);
      else camera = Camera.open();

    } catch (RuntimeException exception) {
      Log.e(TAG, "Cannot open camera. It may be in use by another process.");
      return;
    }

    Log.i(TAG, "Camera open");

    try {

      camera.setPreviewDisplay(holder);

    } catch (IOException exception) {
      Log.e(TAG, "IOException setting display holder");
      camera.release();
      camera = null;
      Log.i(TAG, "Released camera");
      return;
    }
  }
 @SuppressLint("NewApi")
 private void openCamera() {
   if (mCamera == null) {
     Log.d("", "Open new Camera Object");
     if (android.os.Build.VERSION.SDK_INT >= 9) {
       try {
         Log.d("", "Trying to open camera with id = " + VinteractApplication.mCameraIDToOpen);
         mCamera = Camera.open(VinteractApplication.mCameraIDToOpen);
       } catch (RuntimeException RE) {
         RE.printStackTrace();
         finishWithCameraRealease();
       }
     } else {
       mCamera = Camera.open();
     }
     if (mCamera == null) {
       Log.e("", "Error: Sorry your device does not have unused Camera.");
       finishWithCameraRealease();
       return;
     }
     if (customVideoView != null) {
       Log.d("", "Set preview with new camera object onPause");
       customVideoView.setCamera(mCamera);
       customVideoView.setPreviewCallback(this);
     }
   }
 }
Exemplo n.º 5
0
  @SuppressLint("NewApi")
  @Override
  public void surfaceCreated(SurfaceHolder holder) {

    Log.i(TAG, "Opening camera.");
    try {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        int cameraIndex =
            Integer.parseInt(
                PreferenceManager.getDefaultSharedPreferences(getContext())
                    .getString("pref_cameraIndex", "0"));
        camera = Camera.open(cameraIndex);
      } else {
        camera = Camera.open();
      }
    } catch (RuntimeException exception) {
      Log.e(TAG, "Cannot open camera. It may be in use by another process.");
    }
    if (camera != null) {
      try {

        camera.setPreviewDisplay(holder);
        // camera.setPreviewCallback(this);
        camera.setPreviewCallbackWithBuffer(this); // API level 8 (Android 2.2)

      } catch (IOException exception) {
        Log.e(TAG, "Cannot set camera preview display.");
        camera.release();
        camera = null;
      }
    }
  }
Exemplo n.º 6
0
  /* 確認裝置是否有前置鏡頭 */
  private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
      // this device has a camera
      return true;
    } else {
      // no camera on this device
      return false;
    }
  }
  //	/* 開啟相機前至鏡頭設置 */
  private Camera openFrontFacingCameraGingerbread() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
      Camera.getCameraInfo(camIdx, cameraInfo);
      if (checkCameraHardware(MainActivity.this)) {
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
          try {
            cam = Camera.open(camIdx);
          } catch (RuntimeException e) {
            Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
          }
        }
        if (!checkCameraHardware(MainActivity.this)) {
          if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            try {
              cam = Camera.open(camIdx);
            } catch (RuntimeException e) {
              Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
            }
          }
        }
      }
    }

    return cam;
  }
Exemplo n.º 7
0
  public void chooseCamera() {
    // if the camera preview is the front
    if (cameraFront) {
      int cameraId = findBackFacingCamera();
      if (cameraId >= 0) {
        // open the backFacingCamera
        // set a picture callback
        // refresh the preview

        mCamera = Camera.open(cameraId);
        mPicture = getPictureCallback();
        mPreview.refreshCamera(mCamera);
      }
    } else {
      int cameraId = findFrontFacingCamera();
      if (cameraId >= 0) {
        // open the backFacingCamera
        // set a picture callback
        // refresh the preview

        mCamera = Camera.open(cameraId);
        mPicture = getPictureCallback();
        mPreview.refreshCamera(mCamera);
      }
    }
  }
Exemplo n.º 8
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();
    }
  }
Exemplo n.º 9
0
 @Override
 public void onResume() {
   super.onResume();
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
     mCamera = Camera.open(CAMERA);
   } else {
     mCamera = Camera.open();
   }
 }
Exemplo n.º 10
0
  @SuppressLint("NewApi")
  public void changeCameraFacing() {
    // 切换前后摄像头
    int cameraCount = 0;
    CameraInfo cameraInfo = new CameraInfo();
    cameraCount = Camera.getNumberOfCameras(); // 得到摄像头的个数

    for (int i = 0; i < cameraCount; i++) {
      Camera.getCameraInfo(i, cameraInfo); // 得到每一个摄像头的信息
      if (cameraPosition == 1) {
        // 现在是后置,变更为前置
        if (cameraInfo.facing
            == Camera.CameraInfo
                .CAMERA_FACING_FRONT) { // 代表摄像头的方位,CAMERA_FACING_FRONT前置      CAMERA_FACING_BACK后置
          mCamera.setPreviewCallback(null);
          mCamera.stopPreview(); // 停掉原来摄像头的预览
          mCamera.release(); // 释放资源
          mCamera = null; // 取消原来摄像头
          mCamera = Camera.open(i); // 打开当前选中的摄像头
          try {
            mCamera.setPreviewDisplay(mHolder); // 通过surfaceview显示取景画面
            initParameters();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          mCamera.startPreview(); // 开始预览
          cameraPosition = 0;
          break;
        }
      } else {
        // 现在是前置, 变更为后置
        if (cameraInfo.facing
            == Camera.CameraInfo
                .CAMERA_FACING_BACK) { // 代表摄像头的方位,CAMERA_FACING_FRONT前置      CAMERA_FACING_BACK后置
          mCamera.setPreviewCallback(null);
          mCamera.stopPreview(); // 停掉原来摄像头的预览
          mCamera.release(); // 释放资源
          mCamera = null; // 取消原来摄像头
          mCamera = Camera.open(i); // 打开当前选中的摄像头
          try {
            mCamera.setPreviewDisplay(mHolder); // 通过surfaceview显示取景画面
            initParameters();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          mCamera.startPreview(); // 开始预览
          cameraPosition = 1;
          break;
        }
      }
    }
  }
  /**
   * Opens a camera, and attempts to establish preview mode at the specified width and height.
   *
   * <p>Sets mCameraPreviewWidth and mCameraPreviewHeight to the actual width/height of the preview.
   */
  private void openCamera(int desiredWidth, int desiredHeight) {
    if (mCamera != null) {
      throw new RuntimeException("camera already initialized");
    }

    Camera.CameraInfo info = new Camera.CameraInfo();

    // Try to find a back-facing camera (e.g. for videoconferencing).
    int numCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numCameras; i++) {
      Camera.getCameraInfo(i, info);
      //            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {  //TODO: front
      // facing
      if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
        mCamera = Camera.open(i);
        break;
      }
    }
    if (mCamera == null) {
      Log.d(TAG, "No front-facing camera found; opening default");
      mCamera = Camera.open(); // opens first back-facing camera
    }
    if (mCamera == null) {
      throw new RuntimeException("Unable to open camera");
    }
    mCamera.setDisplayOrientation(90); // TODO: set according to front or back facing

    Camera.Parameters parms = mCamera.getParameters();
    //        parms.setRotation(180);

    CameraUtils.choosePreviewSize(parms, desiredWidth, desiredHeight);

    // Give the camera a hint that we're recording video.  This can have a big
    // impact on frame rate.
    parms.setRecordingHint(true);

    // leave the frame rate set to default
    mCamera.setParameters(parms);

    int[] fpsRange = new int[2];
    Camera.Size mCameraPreviewSize = parms.getPreviewSize();
    parms.getPreviewFpsRange(fpsRange);
    String previewFacts = mCameraPreviewSize.width + "x" + mCameraPreviewSize.height;
    if (fpsRange[0] == fpsRange[1]) {
      previewFacts += " @" + (fpsRange[0] / 1000.0) + "fps";
    } else {
      previewFacts += " @[" + (fpsRange[0] / 1000.0) + " - " + (fpsRange[1] / 1000.0) + "] fps";
    }
    TextView text = (TextView) findViewById(R.id.cameraParams_text);
    text.setText(previewFacts);

    mCameraPreviewWidth = mCameraPreviewSize.width;
    mCameraPreviewHeight = mCameraPreviewSize.height;
  }
 // A safe way to get an instance of the Camera object.
 public Camera getCameraInstance(int cameraId) {
   Camera c = null;
   try {
     if (cameraId == -1) {
       c = Camera.open(); // attempt to get a Camera instance
     } else {
       c = Camera.open(cameraId); // attempt to get a Camera instance
     }
   } catch (Exception e) {
     // Camera is not available (in use or does not exist)
   }
   return c; // returns null if camera is unavailable
 }
Exemplo n.º 13
0
  /** Handles camera opening */
  private void openCamera(int which) {
    if (mCamera != null) {
      mCamera.stopPreview();
      mCamera.release();
      mCamera = null;
    }

    if (mCameraId >= 0) {
      Camera.getCameraInfo(mCameraId, mCameraInfo);
      if (which == FRONT) mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
      else mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);

      params = mCamera.getParameters();
      params.setRotation(0);

      /** set focus mode */
      List<String> FocusModes = params.getSupportedFocusModes();
      if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
      }

      mCamera.setParameters(params);
      try {
        if (mSurfaceTexture != null) {
          mCamera.setPreviewTexture(mSurfaceTexture);
          mCamera.startPreview();
        }
      } catch (Exception ex) {
      }
    }

    if (mCamera == null || mSharedData == null) {
      return;
    }

    int orientation = mCameraInfo.orientation + rot_angle;

    Matrix.setRotateM(mSharedData.mOrientationM, 0, orientation, 0f, 0f, 1f);

    Camera.Size size = mCamera.getParameters().getPreviewSize();
    if (orientation % 90 == 0) {
      int w = size.width;
      size.width = size.height;
      size.height = w;
    }

    mSharedData.mAspectRatioPreview[0] = (float) Math.min(size.width, size.height) / size.width;
    mSharedData.mAspectRatioPreview[1] = (float) Math.min(size.width, size.height) / size.height;
  }
Exemplo n.º 14
0
  /**
   * Opens the camera driver and initializes the hardware parameters.
   *
   * @param holder The surface object which the camera will draw preview frames into.
   * @throws IOException Indicates the camera driver failed to open.
   */
  public void openDriver(SurfaceHolder holder) throws IOException {
    if (camera == null) {
      camera = Camera.open();
      if (camera == null) {
        throw new IOException();
      }
      camera.setPreviewDisplay(holder);

      if (!initialized) {
        initialized = true;
        configManager.initFromCameraParameters(camera);
      }
      configManager.setDesiredCameraParameters(camera);

      // FIXME
      // SharedPreferences prefs =
      // PreferenceManager.getDefaultSharedPreferences(context);
      // 是否使用前灯
      // if (prefs.getBoolean(PreferencesActivity.KEY_FRONT_LIGHT, false))
      // {
      // FlashlightManager.enableFlashlight();
      // }
      FlashlightManager.enableFlashlight();
    }
  }
Exemplo n.º 15
0
  public void constructorVideo() {
    // ctx = c;
    // this.channel = chan;

    th =
        new Thread(
            new Runnable() {

              public void run() {
                listen();
              }
            });
    th.start();

    try {
      Thread.sleep(1000);
    } catch (Exception e) {
    }

    ls = new LocalSocket();
    try {
      ls.connect(new LocalSocketAddress(SOCKET_ADDRESS));
    } catch (IOException e) {
      // stop();
    }
    camera = Camera.open();
  }
Exemplo n.º 16
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_camera);

    thumbnail = (ImageView) findViewById(R.id.camera_thumbnail);

    cam = Camera.open();
    final FrameLayout preview = (FrameLayout) findViewById(R.id.cameraPreview);
    Log.d(TAG, preview + "");
    camPreview = new CameraPreview(this, cam);
    preview.addView(camPreview);
    thumbnail.bringToFront();
    changeThumbnail();

    camPreview.setOnTouchListener(
        new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
            if (!isTaken) {
              isTaken = true;
              cam.takePicture(null, null, picJpgListener);
              Toast.makeText(CameraActivity.this, "保存中", Toast.LENGTH_SHORT).show();
            }
            return false;
          }
        });
  }
Exemplo n.º 17
0
  @Override
  public void openCamera(final int cameraId) {
    releaseCamera();

    if (sSharpCameraClass != null) {
      final Method openMethod;
      try {
        openMethod = sSharpCameraClass.getMethod("open", int.class);
      } catch (final NoSuchMethodException e) {
        throw new RuntimeException(e.getMessage(), e);
      }
      try {
        setCamera((Camera) openMethod.invoke(null, cameraId));
      } catch (final IllegalArgumentException e) {
        throw new RuntimeException(e.getMessage(), e);
      } catch (final IllegalAccessException e) {
        throw new RuntimeException(e.getMessage(), e);
      } catch (final InvocationTargetException e) {
        throw new RuntimeException(e.getMessage(), e);
      }
    } else if (cameraId != DEFAULT_CAMERA_ID) {
      throw new RuntimeException();
    } else {
      setCamera(Camera.open());
    }

    setCameraId(cameraId);
    initializeFocusMode();
  }
 private void initCamera() {
   if (!isPreview) {
     camera = Camera.open();
   }
   if (camera != null && !isPreview) {
     streamIt = new StreamIt();
     try {
       Camera.Parameters parameters = camera.getParameters();
       parameters.setPreviewSize(screenWidth, screenHeight); // 设置预览照片的大小	
       parameters.setPreviewFpsRange(20, 30); // 每秒显示20~30帧	
       parameters.setPictureFormat(ImageFormat.NV21); // 设置图片格式	
       parameters.setPictureSize(screenWidth, screenHeight); // 设置照片的大小
       camera.setPreviewDisplay(surfaceHolder); // 通过SurfaceView显示取景画面
       camera.setPreviewCallback(streamIt); // 设置回调的类	
       camera.startPreview(); // 开始预览	
       camera.autoFocus(null); // 自动对焦
     } catch (Exception e) {
       e.printStackTrace();
     }
     isPreview = true;
     Thread postThread2 = new Thread(new postThread());
     postThread2.start();
     System.out.println("线程启动了@@@@@@@@@@@@@@@@@@@@@@@@@@");
   }
 }
Exemplo n.º 19
0
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  public void onResume() {
    addView(previewStrategy.getWidget());

    if (camera == null) {
      cameraId = getHost().getCameraId();

      if (cameraId >= 0) {
        try {
          camera = Camera.open(cameraId);

          if (getActivity().getRequestedOrientation()
              != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
            onOrientationChange.enable();
          }

          setCameraDisplayOrientation();

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH
              && getHost() instanceof Camera.FaceDetectionListener) {
            camera.setFaceDetectionListener((Camera.FaceDetectionListener) getHost());
          }
        } catch (Exception e) {
          getHost().onCameraFail(FailureReason.UNKNOWN);
        }
      } else {
        getHost().onCameraFail(FailureReason.NO_CAMERAS_REPORTED);
      }
    }
  }
  private boolean openCamera(int id) {
    boolean result = false;
    cameraId = id;
    releaseCamera();
    try {
      camera = Camera.open(cameraId);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (camera != null) {
      try {
        setUpCamera(camera);
        camera.setErrorCallback(
            new ErrorCallback() {

              @Override
              public void onError(int error, Camera camera) {}
            });
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
        result = true;
      } catch (IOException e) {
        e.printStackTrace();
        result = false;
        releaseCamera();
      }
    }
    return result;
  }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.v("FlashlightService", "Starting Flash");
    if (cam != null) return START_NOT_STICKY;
    try {
      cam = Camera.open();
      Parameters p = cam.getParameters();
      List<String> flashes = p.getSupportedFlashModes();
      if (flashes == null) return error(this, R.string.err_available);
      if (flashes.contains(Parameters.FLASH_MODE_TORCH))
        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
      else if (flashes.contains(Parameters.FLASH_MODE_ON)) p.setFlashMode(Parameters.FLASH_MODE_ON);
      else return error(this, R.string.err_available);
      cam.setParameters(p);
      // Needed for some devices.
      cam.setPreviewTexture(new SurfaceTexture(0));
      // Needed for some more devices.
      cam.startPreview();

      // Keep phone awake with screen off
      wl =
          ((PowerManager) getSystemService(Context.POWER_SERVICE))
              .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "FlashlightService");
      if (wl != null && !wl.isHeld()) wl.acquire();
      return START_NOT_STICKY;
    } catch (Exception e) {
      return error(this, R.string.err_access);
    }
  }
Exemplo n.º 22
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.º 23
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.º 24
0
  public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, acquire the camera and tell it where
    // to draw.
    camera = Camera.open();
    try {
      camera.setPreviewDisplay(holder);

      camera.setPreviewCallback(
          new PreviewCallback() {

            public void onPreviewFrame(byte[] data, Camera arg1) {
              FileOutputStream outStream = null;
              try {
                outStream =
                    new FileOutputStream(
                        String.format("/sdcard/%d.jpg", System.currentTimeMillis()));
                outStream.write(data);
                outStream.close();
                Log.d(TAG, "onPreviewFrame - wrote bytes: " + data.length);
              } catch (FileNotFoundException e) {
                e.printStackTrace();
              } catch (IOException e) {
                e.printStackTrace();
              } finally {
              }
              Preview.this.invalidate();
            }
          });
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 25
0
 public void onSurfaceTextureAvailable(
     SurfaceTexture paramSurfaceTexture, int paramInt1, int paramInt2) {
   if (VideoCaptureFragment.h(a) == null) {
     VideoCaptureFragment.a(a, Camera.open());
     Object localObject1 = VideoCaptureFragment.h(a).getParameters();
     Object localObject2 = VideoCaptureFragment.i(a);
     Camera localCamera = VideoCaptureFragment.h(a);
     localCamera.getClass();
     localObject2 = new Camera.Size(localCamera, videoFrameWidth, videoFrameHeight);
     if (((Camera.Parameters) localObject1).getSupportedPreviewSizes().contains(localObject2)) {
       ((Camera.Parameters) localObject1).setPreviewSize(width, height);
       VideoCaptureFragment.h(a).setParameters((Camera.Parameters) localObject1);
     }
     localObject1 = ((Camera.Parameters) localObject1).getPreviewSize();
     VideoCaptureFragment.a(a, width, height);
     VideoCaptureFragment.h(a).setDisplayOrientation(VideoCaptureFragment.j(a));
     try {
       VideoCaptureFragment.h(a).setPreviewTexture(paramSurfaceTexture);
       VideoCaptureFragment.h(a).startPreview();
       paramSurfaceTexture = VideoCaptureFragment.h(a).getParameters().getSupportedFocusModes();
       if (CameraWrangler.a(VideoCaptureFragment.h(a))) {
         VideoCaptureFragment.h(a).autoFocus(null);
         return;
       }
       if (paramSurfaceTexture.contains("continuous-video")) {
         paramSurfaceTexture = VideoCaptureFragment.h(a).getParameters();
         paramSurfaceTexture.setFocusMode("continuous-video");
         VideoCaptureFragment.h(a).setParameters(paramSurfaceTexture);
         return;
       }
     } catch (IOException paramSurfaceTexture) {
       YelpLog.error(paramSurfaceTexture);
     }
   }
 }
  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;
    }
  }
  private void checkRearCamera() {
    Camera camera = null;
    try {
      camera = Camera.open();
      if (camera != null) {
        assertAvailable(PackageManager.FEATURE_CAMERA);

        Camera.Parameters params = camera.getParameters();
        if (params.getSupportedFocusModes().contains(Parameters.FOCUS_MODE_AUTO)) {
          assertAvailable(PackageManager.FEATURE_CAMERA_AUTOFOCUS);
        } else {
          assertNotAvailable(PackageManager.FEATURE_CAMERA_AUTOFOCUS);
        }

        if (params.getFlashMode() != null) {
          assertAvailable(PackageManager.FEATURE_CAMERA_FLASH);
        } else {
          assertNotAvailable(PackageManager.FEATURE_CAMERA_FLASH);
        }
      } else {
        assertNotAvailable(PackageManager.FEATURE_CAMERA);
        assertNotAvailable(PackageManager.FEATURE_CAMERA_AUTOFOCUS);
        assertNotAvailable(PackageManager.FEATURE_CAMERA_FLASH);
      }
    } finally {
      if (camera != null) {
        camera.release();
      }
    }
  }
Exemplo n.º 28
0
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  @Override
  protected void onResume() {
    super.onResume();
    Log.i(TAG, "onResume");
    currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
    camera = Camera.open(currentCameraId);
    int pictureHeight = camera.getParameters().getPictureSize().height;
    int pictureWidth = camera.getParameters().getPictureSize().width;
    FrameLayout surfWrap = (FrameLayout) findViewById(R.id.surface_wrapper);
    surfWrap.setLayoutParams(new FrameLayout.LayoutParams(pictureHeight, pictureWidth));

    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

    int orientation = display.getRotation();
    Log.i("Rotation", String.valueOf(orientation));
    if (orientation == Surface.ROTATION_90) {
      // TODO: add logic for landscape mode here
      Log.i("Rotation", "Landscape mode");
      displayRotation = 0;
    } else if (orientation == Surface.ROTATION_270) {

      displayRotation = 270;
    } else {
      Log.i("Rotation", "Portrait mode");
      displayRotation = 90;
    }
  }
Exemplo n.º 29
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.º 30
0
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
      case R.id.switch_cam:
        // Release this camera -> mCameraCurrentlyLocked
        if (mCamera != null) {
          mCamera.stopPreview();
          mPreview.setCamera(null);
          mCamera.release();
          mCamera = null;
        }

        // Acquire the next camera and request Preview to reconfigure
        // parameters.
        mCamera = Camera.open((mCameraCurrentlyLocked + 1) % mNumberOfCameras);
        mCameraCurrentlyLocked = (mCameraCurrentlyLocked + 1) % mNumberOfCameras;
        mPreview.switchCamera(mCamera);

        // Start the preview
        mCamera.startPreview();
        return true;
      case android.R.id.home:
        Intent intent = new Intent(this.getActivity(), MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);

      default:
        return super.onOptionsItemSelected(item);
    }
  }