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
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    mHolder.setFixedSize(w, h);
    // Start the preview
    Parameters params = mCamera.getParameters();
    previewHeight = params.getPreviewSize().height;
    previewWidth = params.getPreviewSize().width;
    previewFormat = params.getPreviewFormat();

    // Crop the edges of the picture to reduce the image size
    r =
        new Rect(
            previewShrink,
            previewShrink,
            previewWidth - previewShrink,
            previewHeight - previewShrink);

    mCallbackBuffer = new byte[497664];

    mCamera.setParameters(params);
    mCamera.setPreviewCallbackWithBuffer(
        new PreviewCallback() {
          public void onPreviewFrame(byte[] imageData, Camera arg1) {
            convWorker.nextFrame(imageData);
          }
        });
    mCamera.addCallbackBuffer(mCallbackBuffer);
    mCamera.startPreview();
    setTorchMode(mTorchMode);
  }
Exemplo n.º 3
0
 public void offFlashLight() {
   if (camera != null) {
     Parameters parameter = camera.getParameters();
     parameter.setFlashMode(Parameters.FLASH_MODE_OFF);
     camera.setParameters(parameter);
   }
 }
Exemplo n.º 4
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.º 5
0
  private void updateCameraParametersPreference() {
    // Set picture size.
    String pictureSize = mPreferences.getString(KEY_PICTURE_SIZE, null);
    if (pictureSize == null) {
      initialCameraPictureSize(this, mParameters);
    } else {
      List<Size> supported = mParameters.getSupportedPictureSizes();
      setCameraPictureSize(pictureSize, supported, mParameters);
    }

    // Set the preview frame aspect ratio according to the picture size.
    Size size = mParameters.getPictureSize();
    PreviewFrameLayout frameLayout = (PreviewFrameLayout) findViewById(R.id.frame_layout);
    frameLayout.setAspectRatio((double) size.width / size.height);

    // Set a preview size that is closest to the viewfinder height and has
    // the right aspect ratio.
    List<Size> sizes = mParameters.getSupportedPreviewSizes();
    Size optimalSize = getOptimalPreviewSize(sizes, (double) size.width / size.height);
    if (optimalSize != null) {
      mParameters.setPreviewSize(optimalSize.width, optimalSize.height);
    }
    mParameters.setJpegQuality(85);
    updateFlashModeParameter();
  }
Exemplo n.º 6
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());
    }
  }
  @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.º 8
0
  /** Turns the LED on or off if phone has one. */
  public synchronized void setFlashState(boolean state) {
    // If the camera has already been opened, we apply the change immediately
    if (mCamera != null) {

      if (mStreaming && mMode == MODE_MEDIARECORDER_API) {
        lockCamera();
      }

      Parameters parameters = mCamera.getParameters();

      // We test if the phone has a flash
      if (parameters.getFlashMode() == null) {
        // The phone has no flash or the choosen camera can not toggle the flash
        throw new RuntimeException("Can't turn the flash on !");
      } else {
        parameters.setFlashMode(state ? Parameters.FLASH_MODE_TORCH : Parameters.FLASH_MODE_OFF);
        try {
          mCamera.setParameters(parameters);
          mFlashEnabled = state;
        } catch (RuntimeException e) {
          mFlashEnabled = false;
          throw new RuntimeException("Can't turn the flash on !");
        } finally {
          if (mStreaming && mMode == MODE_MEDIARECORDER_API) {
            unlockCamera();
          }
        }
      }
    } else {
      mFlashEnabled = state;
    }
  }
Exemplo n.º 9
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.º 10
0
 @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
 public static boolean isFocusAreaSupported(Parameters params) {
   if (ApiHelper.HAS_CAMERA_FOCUS_AREA) {
     return (params.getMaxNumFocusAreas() > 0
         && isSupported(Parameters.FOCUS_MODE_AUTO, params.getSupportedFocusModes()));
   }
   return false;
 }
Exemplo n.º 11
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;
      }
    }
  }
Exemplo n.º 12
0
 private void updateCameraParametersInitialize() {
   // Reset preview frame rate to the maximum because it may be lowered by
   // video camera application.
   List<Integer> frameRates = mParameters.getSupportedPreviewFrameRates();
   if (frameRates != null) {
     Integer max = Collections.max(frameRates);
     mParameters.setPreviewFrameRate(max);
   }
 }
  private void showFlashButton(Parameters params) {
    boolean showFlash =
        (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
                && params.getFlashMode() != null)
            && params.getSupportedFlashModes() != null
            && params.getSupportedFocusModes().size() > 1;

    flashCameraButton.setVisibility(showFlash ? View.VISIBLE : View.INVISIBLE);
  }
 private void flashOff() {
   Toast.makeText(getApplicationContext(), "FLASH OFF", Toast.LENGTH_SHORT).show();
   final Parameters p = camera.getParameters();
   p.setFlashMode(Parameters.FLASH_MODE_OFF);
   camera.setParameters(p);
   isFlashOn = false;
   button.setText("Torch OFF");
   flashTimerHandler.removeCallbacks(updateFlashTimerThread);
 }
Exemplo n.º 15
0
 private void setTorchMode(boolean on) {
   if (mCamera != null) {
     Parameters params = mCamera.getParameters();
     if (on) {
       params.setFlashMode(Parameters.FLASH_MODE_TORCH);
     } else {
       params.setFlashMode(Parameters.FLASH_MODE_AUTO);
     }
     mTorchMode = on;
     mCamera.setParameters(params);
   }
 }
Exemplo n.º 16
0
  public void switchFlashLight() {
    if (camera != null) {
      Parameters parameter = camera.getParameters();
      if (parameter.getFlashMode().equals(Parameters.FLASH_MODE_TORCH)) {
        parameter.setFlashMode(Parameters.FLASH_MODE_OFF);
      } else {
        parameter.setFlashMode(Parameters.FLASH_MODE_TORCH);
      }

      camera.setParameters(parameter);
    }
  }
 private void flashOnButton() {
   if (camera != null) {
     try {
       Parameters param = camera.getParameters();
       param.setFlashMode(!flashmode ? Parameters.FLASH_MODE_TORCH : Parameters.FLASH_MODE_OFF);
       camera.setParameters(param);
       flashmode = !flashmode;
     } catch (Exception e) {
       // TODO: handle exception
     }
   }
 }
Exemplo n.º 18
0
        public void onPictureTaken(byte[] data, Camera camera) {
          // TODO Auto-generated method stub
          Log.d("takephoto", "lalalalalalal!");
          mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
          //			File datafile = new File(android.os.Environment.getExternalStorageDirectory()
          //					+ "/capstone/camera" + Integer.toString(num++) + ".dat");

          File imagefile =
              new File(
                  android.os.Environment.getExternalStorageDirectory()
                      + "/capstone/camera"
                      + Integer.toString(num++)
                      + ".jpg");
          try {
            BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(imagefile));
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, os);

            os.flush();
            os.close();

            //				os = new BufferedOutputStream(new FileOutputStream(datafile));
            //				os.write(data);
            //				os.flush();
            //				os.close();

            //				Canvas canvas = holder.lockCanvas();
            //				canvas.drawBitmap(mBitmap, 0, 0, null);
            //				holder.unlockCanvasAndPost(canvas);
            mCamera.startPreview();

            float[] dis = new float[3];
            Parameters parameters = mCamera.getParameters();
            parameters.getFocusDistances(dis);
            Log.d(
                "focal dis",
                Float.toString(dis[0])
                    + " "
                    + Float.toString(dis[1])
                    + " "
                    + Float.toString(dis[2]));

            Log.d("focal length", Float.toString(parameters.getFocalLength()));

          } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          Log.d("takephoto", "lalalalalalal!");
        }
 private void flashOn() {
   Toast.makeText(
           getApplicationContext(),
           flashTimerOptionValues[currentFlashTimerIndex] + " FLASH ON",
           Toast.LENGTH_SHORT)
       .show();
   final Parameters p = camera.getParameters();
   p.setFlashMode(Parameters.FLASH_MODE_TORCH);
   camera.setParameters(p);
   isFlashOn = true;
   isFlashOnTimerCheck = true;
   button.setText("Torch ON");
 }
Exemplo n.º 20
0
  private void refreshCamera() {
    if (isopend == false) {
      camera = Camera.open();
      Parameters params = camera.getParameters();
      params.setFlashMode(Parameters.FLASH_MODE_TORCH);
      camera.setParameters(params);
      camera.startPreview(); // 开始亮灯

      isopend = true;
    } else {
      camera.stopPreview(); // 关掉亮灯
      camera.release(); // 关掉照相机
      isopend = false;
    }
  }
 public static boolean isZoomSupported(Parameters params) {
   if (zoomAvaible) {
     return params.isZoomSupported();
   } else {
     return false;
   }
 }
Exemplo n.º 22
0
 @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
 public static boolean isMeteringAreaSupported(Parameters params) {
   if (ApiHelper.HAS_CAMERA_METERING_AREA) {
     return params.getMaxNumMeteringAreas() > 0;
   }
   return false;
 }
 public static int getMaxZoom(Parameters params) {
   if (zoomAvaible) {
     return params.getMaxZoom();
   } else {
     return -1;
   }
 }
Exemplo n.º 24
0
 /**
  * 根据点击的按钮,设置camera闪光灯模式
  *
  * @param i
  */
 private void checkFlash(Integer i) {
   int j = i;
   try {
     Parameters par = mCamera.getParameters();
     if (j == CRes.plugin_camera_flash_drawale_auto) {
       par.setFlashMode(Parameters.FLASH_MODE_AUTO);
     } else if (j == CRes.plugin_camera_flash_drawale_open) {
       par.setFlashMode(Parameters.FLASH_MODE_ON);
     } else if (j == CRes.plugin_camera_flash_drawale_close) {
       par.setFlashMode(Parameters.FLASH_MODE_OFF);
     }
     mCamera.setParameters(par);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 25
0
 private void updateFlashModeParameter() {
   String flashMode =
       getFlashParameter(
           SharedPreferencesHelper.getSharedPreferences()
               .getInt(getString(R.string.pref_key_flash_res_id), 0));
   mParameters.setFlashMode(flashMode);
 }
Exemplo n.º 26
0
  public void flashOn(Context context) {

    if (!isOn) {
      boolean hasFlash =
          context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

      if (hasFlash) {
        isOn = true;
        cam = Camera.open();
        Parameters p = cam.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
        cam.setParameters(p);
        cam.startPreview();
      }
    }
  }
  // Turning Off flash
  private void turnOffFlash() {

    frameAnimation.stop();
    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
  }
Exemplo n.º 28
0
 public static void dumpParameters(Parameters parameters) {
   String flattened = parameters.flatten();
   StringTokenizer tokenizer = new StringTokenizer(flattened, ";");
   Log.d(TAG, "Dump all camera parameters:");
   while (tokenizer.hasMoreElements()) {
     Log.d(TAG, tokenizer.nextToken());
   }
 }
Exemplo n.º 29
0
  public void setAutoFocus() {
    Parameters params = cam.getParameters();
    List<String> focusModes = params.getSupportedFocusModes();
    try {
      if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);

      } else if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);

      } else if (focusModes.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) {
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
      }
    } catch (Exception ex) {

    }
  }
Exemplo n.º 30
0
 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
   // Now that the size is known, set up the camera parameters and begin
   // the preview.
   parameters.setPreviewSize(w, h);
   // set the camera's settings
   mCamera.setParameters(parameters);
   mCamera.startPreview();
 }