android.hardware.Camera类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(257)

本文整理了Java中android.hardware.Camera类的一些代码示例,展示了Camera类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Camera类的具体详情如下:
包路径:android.hardware.Camera
类名称:Camera

Camera介绍

暂无

代码示例

代码示例来源:origin: vondear/RxTool

/**
 * 打开闪光灯
 *
 * @return
 */
public static void openFlashLight() {
  try {
    if (camera == null) {
      camera = Camera.open();
      camera.startPreview();
    }
    Camera.Parameters parameters = camera.getParameters();
    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    camera.setParameters(parameters);
  } catch (Exception e) {
    e.printStackTrace();
  }
}

代码示例来源:origin: stackoverflow.com

SurfaceView preview = (SurfaceView) findViewById(R.id.PREVIEW);
SurfaceHolder mHolder = preview.getHolder();
mHolder.addCallback(this);
Camera mCamera = Camera.open();
mCamera.setPreviewDisplay(mHolder);

// Turn on LED  
Parameters params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCamera.setParameters(params);      
mCamera.startPreview();

...

// Turn off LED
Parameters params = mCamera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
mCamera.setParameters(params);
mCamera.stopPreview();
mCamera.release();

代码示例来源:origin: vondear/RxTool

/**
 * This rewrites {@link #mCameraId} and {@link #mCameraInfo}.
 */
private void chooseCamera() {
  for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) {
    Camera.getCameraInfo(i, mCameraInfo);
    if (mCameraInfo.facing == mFacing) {
      mCameraId = i;
      return;
    }
  }
  mCameraId = INVALID_CAMERA_ID;
}

代码示例来源:origin: stackoverflow.com

Camera cam = Camera.open();     
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();

代码示例来源:origin: journeyapps/zxing-android-embedded

public void setPreview(Camera camera) throws IOException {
    if(surfaceHolder != null) {
      camera.setPreviewDisplay(surfaceHolder);
    } else {
      camera.setPreviewTexture(surfaceTexture);
    }
  }
}

代码示例来源:origin: stackoverflow.com

PictureCallback mPicture = new PictureCallback() {
  @Override
  public void onPictureTaken(byte[] data, Camera camera) {
    pictureFile = getOutputMediaFile();
    camera.startPreview();

    if (pictureFile == null) {
      //no path to picture, return
      safeToTakePicture = true;
      return;
    }
    try {
      FileOutputStream fos = new FileOutputStream(pictureFile);
      fos.write(data);
      fos.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();              //<-------- show exception
    } catch (IOException e) {
      e.printStackTrace();              //<-------- show exception
    }

    //finished saving picture 
    safeToTakePicture = true;
  }
};

代码示例来源:origin: stackoverflow.com

import android.graphics.YuvImage;

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
  try {
    Camera.Parameters parameters = camera.getParameters();
    Size size = parameters.getPreviewSize();
    YuvImage image = new YuvImage(data, parameters.getPreviewFormat(),
        size.width, size.height, null);
    File file = new File(Environment.getExternalStorageDirectory()
        .getPath() + "/out.jpg");
    FileOutputStream filecon = new FileOutputStream(file);
    image.compressToJpeg(
        new Rect(0, 0, image.getWidth(), image.getHeight()), 90,
        filecon);
  } catch (FileNotFoundException e) {
    Toast toast = Toast
        .makeText(getBaseContext(), e.getMessage(), 1000);
    toast.show();
  }
}

代码示例来源:origin: abhn/marvel

@Override
  public void onPictureTaken(byte[] data, Camera camera) {
    Log.i(TAG, "Saving a bitmap to file");
    Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length);
    try {
      FileOutputStream out = new FileOutputStream(mPictureFileName);
      picture.compress(Bitmap.CompressFormat.JPEG, 90, out);
      picture.recycle();
      mCamera.startPreview();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
};

代码示例来源:origin: WangShuo1143368701/VideoView

@Override
public void surfaceDestroyed(SurfaceHolder arg0)
{
  m_camera.setPreviewCallback(null);
  m_camera.release();
  m_camera = null; 
  avcCodec.close();
  try {
    file.flush();
    file.close();
  } catch (IOException e) {
    Log.d("Fuck", "File close error");
    e.printStackTrace();
  }
}

代码示例来源:origin: daniulive/SmarterStreaming

mCamera.stopPreview();
  parameters = mCamera.getParameters();
} catch (Exception e) {
parameters.setPreviewSize(videoWidth, videoHeight);
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setPreviewFormat(PixelFormat.YCbCr_420_SP);
mCamera.setParameters(parameters);
int bufferSize = (((videoWidth|0xf)+1) * videoHeight * ImageFormat.getBitsPerPixel(parameters.getPreviewFormat())) / 8;
mCamera.addCallbackBuffer(new byte[bufferSize]);
mCamera.setPreviewCallbackWithBuffer(this);
try {
  mCamera.setPreviewDisplay(holder);
} catch (Exception ex) {
    mCamera.release();
    mCamera = null;
mCamera.startPreview();
mCamera.autoFocus(myAutoFocusCallback);
mPreviewRunning = true;

代码示例来源:origin: tvbarthel/ChaseWhisplyProject

@Override
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.setPreviewDisplay(mHolder);
    final Camera.Parameters parameters = mCamera.getParameters();
    final Camera.Size size = getBestPreviewSize(w, h);
    parameters.setPreviewSize(size.width, size.height);
    mCamera.setParameters(parameters);
    mCamera.startPreview();
  } catch (Exception e) {
    Log.d(TAG, "Error starting camera preview: " + e.getMessage());
  }
}

代码示例来源:origin: stackoverflow.com

mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
for(Camera.Size str: mSupportedPreviewSizes)
    Log.e(TAG, str.width + "/" + str.height);
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
if (mHolder.getSurface() == null){
  mCamera.stopPreview();
} catch (Exception e){
  Camera.Parameters parameters = mCamera.getParameters();
  parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
  mCamera.setParameters(parameters);
  mCamera.setDisplayOrientation(90);
  mCamera.setPreviewDisplay(mHolder);
  mCamera.startPreview();

代码示例来源:origin: z-chu/HeartRate

@NeedsPermission(Manifest.permission.CAMERA)
void showCamera() {
  mCamera = Camera.open();
  mCamera.setDisplayOrientation(90);
  if (parameters == null) {
    parameters = mCamera.getParameters();
    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);//打开闪光灯
  }
  if (mPreview == null) {
    mPreview = (SurfaceView) findViewById(R.id.sv_preview);
    mPreviewHolder = mPreview.getHolder();
    mPreviewHolder.addCallback(mSurfaceCallback);
    mPreviewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
  }
  mWakeLock.acquire();
  if (parameters != null) {
    try {
      mCamera.setPreviewDisplay(mPreviewHolder);
    } catch (IOException e) {
      e.printStackTrace();
    }
    mCamera.setPreviewCallback(mPreviewCallback);
    mCamera.setParameters(parameters);
    mCamera.startPreview();
  }
}

代码示例来源:origin: stackoverflow.com

surface_holder = surface_view.getHolder();
surface_holder.addCallback(sh_callback);
       mCamera.stopPreview();
       mCamera.release();
       mCamera = null;
          mCamera.setPreviewDisplay(holder);  
       } catch (IOException exception) {  
          mCamera.release();  
          mCamera = null;  
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
      mCamera.startPreview();

代码示例来源:origin: stackoverflow.com

mCamera = Camera.open();
  surfaceView = (SurfaceView) findViewById(R.id.surface_camera);
  surfaceHolder = surfaceView.getHolder();
  surfaceHolder.addCallback(this);
  surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  mCamera.unlock();
  mrec.setPreviewDisplay(surfaceHolder.getSurface());
  mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  mrec.setAudioSource(MediaRecorder.AudioSource.MIC); 
  mrec.stop();
  mrec.release();
  mCamera.release();
    mCamera.lock();           // lock camera for later use
    mCamera.release();        // release the camera for other applications
    mCamera = null;
public void surfaceCreated(SurfaceHolder holder) {
  if (mCamera != null){
    Parameters params = mCamera.getParameters();
    mCamera.setParameters(params);
  mCamera.stopPreview();
  mCamera.release();

代码示例来源:origin: stackoverflow.com

Camera camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(p);
SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surfaceView1); //add this to your xml view
SurfaceHolder surfaceHolder = surfaceView.getHolder()
surfaceHolder.addCallback(this);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();

代码示例来源:origin: stackoverflow.com

mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  Camera.Parameters parameters = mCamera.getParameters();
  List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
  Camera.Size previewSize = null;
  float closestRatio = Float.MAX_VALUE;
  parameters.setPreviewSize(previewSize.width, previewSize.height);
  mCamera.setParameters(parameters);
  mCamera.setPreviewDisplay(mHolder);
  mCamera.startPreview();
} catch (IOException e) {
  Log.d(TAG, "Error setting camera preview: " + e.getMessage());

代码示例来源:origin: stackoverflow.com

setContentView(R.layout.main);
cameraView = (SurfaceView) this.findViewById(R.id.CameraView);
surfaceHolder = cameraView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);
try
 camera.setPreviewDisplay(holder);
 camera.release();
camera.startPreview();

代码示例来源:origin: googlesamples/android-vision

/**
 * 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;
}

代码示例来源:origin: stackoverflow.com

myCamera.unlock();
  myCamera.lock();           // lock camera for later use
  myCamera.release();        // release the camera for other applications
  myCamera = null;
  mHolder.addCallback(this);
  mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mCamera.setPreviewDisplay(mHolder);
    mCamera.startPreview();
    mCamera.setPreviewDisplay(holder);
    mCamera.startPreview();
  } catch (IOException e) {

相关文章