Exemplo n.º 1
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;
        }
      }
    }
  }
Exemplo n.º 2
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();
    }
  }
  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());
    }
  }
Exemplo n.º 4
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;
    }
  }
Exemplo n.º 5
0
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    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
    }

    // Sets preview size.
    Camera.Parameters parameters = mCamera.getParameters();
    List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
    Camera.Size cs = sizes.get(0);
    parameters.setPreviewSize(cs.width, cs.height);
    mCamera.setParameters(parameters);

    // start preview with new settings
    try {
      mCamera.setPreviewDisplay(mHolder);
      mCamera.startPreview();

    } catch (Exception e) {
      Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
    Log.e(TAG, "SurfaceChanged.");
  }
 // Called by native code.  Returns true when camera is known to be stopped.
 private synchronized boolean stopCapture() {
   Log.d(TAG, "stopCapture");
   if (camera == null) {
     throw new RuntimeException("Camera is already stopped!");
   }
   Throwable error = null;
   try {
     camera.stopPreview();
     camera.setPreviewCallbackWithBuffer(null);
     if (localPreview != null) {
       localPreview.removeCallback(this);
       camera.setPreviewDisplay(null);
     } else {
       camera.setPreviewTexture(null);
     }
     camera.release();
     camera = null;
     return true;
   } catch (IOException e) {
     error = e;
   } catch (RuntimeException e) {
     error = e;
   }
   Log.e(TAG, "Failed to stop camera", error);
   return false;
 }
Exemplo n.º 7
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.

    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();
      setCameraDisplayOrientation(
          ((Activity) this.getContext()), Camera.CameraInfo.CAMERA_FACING_FRONT, mCamera);
      mCamera.setParameters(parameters);

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

    } catch (Exception e) {
      Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
  }
  private void initPreview(int width, int height) {
    if (camera != null && previewHolder.getSurface() != null) {
      try {
        camera.setPreviewDisplay(previewHolder);
      } catch (Throwable t) {

      }

      if (!cameraConfigured) {
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size size = getBestPreviewSize(width, height, parameters);
        Camera.Size pictureSize = getLargestPictureSize(parameters);

        if (size != null && pictureSize != null) {
          if (parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO)) {

            // set the focus mode
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
          }
          parameters.setPreviewSize(size.width, size.height);
          parameters.setPictureSize(pictureSize.width, pictureSize.height);
          parameters.setPictureFormat(ImageFormat.JPEG);
          camera.setParameters(parameters);
          cameraConfigured = true;
        }
      }
    }
  }
Exemplo n.º 9
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.º 10
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.º 11
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 {
    Camera theCamera = camera;
    if (theCamera == null) {
      theCamera = Camera.open();
      if (theCamera == null) {
        throw new IOException();
      }
      camera = theCamera;
    }
    theCamera.setPreviewDisplay(holder);

    if (!initialized) {
      initialized = true;
      configManager.initFromCameraParameters(theCamera);
      if (requestedFramingRectWidth > 0 && requestedFramingRectHeight > 0) {
        setManualFramingRect(requestedFramingRectWidth, requestedFramingRectHeight);
        requestedFramingRectWidth = 0;
        requestedFramingRectHeight = 0;
      }
    }
    configManager.setDesiredCameraParameters(theCamera);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    reverseImage = prefs.getBoolean(KEY_REVERSE_IMAGE, false);
  }
  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.º 13
0
 /** 给摄像头设置好一系列相关参数,并开始预览 */
 private void initCamera() {
   azLog.log("isPreView", "" + isPreview);
   if (mCamera != null && !isPreview) {
     try {
       Camera.Parameters parameters = mCamera.getParameters();
       // 设置预览照片的大小
       parameters.setPreviewSize(mPreviewWidth, mPreviewHeight);
       // 设置预览照片时每秒显示多少帧的最小值和最大值
       parameters.setPreviewFpsRange(mFrameMin, mFrameMax);
       // 设置图片格式
       parameters.setPictureFormat(mImageFormat);
       // 设置JPG照片的质量
       parameters.set("jpeg-quality", mImageQuality);
       // 设置照片的大小
       parameters.setPictureSize(mPictureWidth, mPictureHeight);
       // 通过SurfaceView显示取景画面
       mCamera.setPreviewDisplay(mSurfaceHolder);
       // 开始预览
       mCamera.startPreview();
       azLog.log("AzCamera", "摄像头打开");
     } catch (IOException e) {
       e.printStackTrace();
     }
     isPreview = true;
   }
 }
Exemplo n.º 14
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.º 15
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.º 16
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.º 17
0
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    if (mHolder.getSurface() == null) {
      return;
    }

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

    if (mSupportedPreviewSizes != null) {
      mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, w, h);
    }

    // start preview with new settings
    try {

      mCamera.setPreviewDisplay(mHolder);

      Parameters params = mCamera.getParameters();
      // params.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
      params.setPreviewSize(
          mSupportedPreviewSizes.get(0).width, mSupportedPreviewSizes.get(0).height);
      mCamera.setParameters(params);
      mCamera.startPreview();

      Log.d(TAG, "Preview Size: " + mPreviewSize.width + "X" + mPreviewSize.height);

    } catch (Exception e) {
      Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
  }
 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("线程启动了@@@@@@@@@@@@@@@@@@@@@@@@@@");
   }
 }
  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.º 20
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.
    Log.i(TAG, "srface changed");
    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.setPreviewDisplay(mHolder);
      mCamera.startPreview();

    } catch (Exception e) {
      Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
  }
Exemplo n.º 21
0
  /**
   * Opens the camera and starts sending preview frames to the underlying detector. The preview
   * frames are not displayed.
   *
   * @throws IOException if the camera's preview texture or display could not be initialized
   */
  @RequiresPermission(Manifest.permission.CAMERA)
  public CameraSource start() throws IOException {
    synchronized (mCameraLock) {
      if (mCamera != null) {
        return this;
      }

      mCamera = createCamera();

      // SurfaceTexture was introduced in Honeycomb (11), so if we are running and
      // old version of Android. fall back to use SurfaceView.
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        mDummySurfaceTexture = new SurfaceTexture(DUMMY_TEXTURE_NAME);
        mCamera.setPreviewTexture(mDummySurfaceTexture);
      } else {
        mDummySurfaceView = new SurfaceView(mContext);
        mCamera.setPreviewDisplay(mDummySurfaceView.getHolder());
      }
      mCamera.startPreview();

      mProcessingThread = new Thread(mFrameProcessor);
      mFrameProcessor.setActive(true);
      mProcessingThread.start();
    }
    return this;
  }
  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;
  }
Exemplo n.º 23
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.º 24
0
 void initCameraPreview() {
   try {
     _camera.setPreviewDisplay(_holder);
     _camera.startPreview();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
 public void surfaceCreated(SurfaceHolder holder) {
   // The Surface has been created, now tell the camera where to draw the preview.
   try {
     mCamera.setPreviewDisplay(holder);
   } catch (IOException e) {
     Log.d("DBG", "Error setting camera preview: " + e.getMessage());
   }
 }
Exemplo n.º 26
0
 public void surfaceCreated(SurfaceHolder holder) {
   mCamera = Camera.open();
   try {
     mCamera.setPreviewDisplay(holder);
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 27
0
 private void setPreviewDisplay(SurfaceHolder holder) {
   try {
     mCameraDevice.setPreviewDisplay(holder);
   } catch (Throwable ex) {
     closeCamera();
     throw new RuntimeException("setPreviewDisplay failed", ex);
   }
 }
Exemplo n.º 28
0
 public void surfaceCreated(SurfaceHolder holder) {
   try {
     camera.setPreviewDisplay(previewHolder);
   } catch (Throwable t) {
     Log.e("PreviewDemo-surfaceCallback", "Exception in setPreviewDisplay()", t);
     Toast.makeText(CameraActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
   }
 }
Exemplo n.º 29
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;
      }
    }
  }
 /** {@inheritDoc} */
 @Override
 public void surfaceCreated(SurfaceHolder holder) {
   try {
     camera.setPreviewDisplay(previewHolder);
     camera.setPreviewCallback(previewCallback);
   } catch (Throwable t) {
     Log.e("PreviewDemo-surfaceCallback", "Exception in setPreviewDisplay()", t);
   }
 }