android - 不打开相机可以拍照吗?
问题描述
我可以在不打开 Android 相机的情况下拍照吗?
我想从安卓相机捕捉图像,但我不想打开相机预览?
任何人都可以在这里帮助我提供代码片段吗?
解决方案
下面是一个关于如何在没有 SurfaceView 的情况下在后台捕获图像的示例。
Intent front_translucent = new Intent(getApplication()
.getApplicationContext(), CameraService.class);
front_translucent.putExtra("Front_Request", true);
front_translucent.putExtra("Quality_Mode",
camCapture.getQuality());
getApplication().getApplicationContext().startService(
front_translucent);
public class CamerService extends Service implements
SurfaceHolder.Callback {
// Camera variables
// a surface holder
// a variable to control the camera
private Camera mCamera;
// the camera parameters
private Parameters parameters;
private Bitmap bmp;
FileOutputStream fo;
private String FLASH_MODE;
private int QUALITY_MODE = 0;
private boolean isFrontCamRequest = false;
private Camera.Size pictureSize;
SurfaceView sv;
private SurfaceHolder sHolder;
private WindowManager windowManager;
WindowManager.LayoutParams params;
public Intent cameraIntent;
SharedPreferences pref;
Editor editor;
int width = 0, height = 0;
/** Called when the activity is first created. */
@Override
public void onCreate() {
super.onCreate();
}
private Camera openFrontFacingCameraGingerbread() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
}
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 (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
cam = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e("Camera",
"Camera failed to open: " + e.getLocalizedMessage());
/*
* Toast.makeText(getApplicationContext(),
* "Front Camera failed to open", Toast.LENGTH_LONG)
* .show();
*/
}
}
}
return cam;
}
private void setBesttPictureResolution() {
// get biggest picture size
width = pref.getInt("Picture_Width", 0);
height = pref.getInt("Picture_height", 0);
if (width == 0 | height == 0) {
pictureSize = getBiggesttPictureSize(parameters);
if (pictureSize != null)
parameters
.setPictureSize(pictureSize.width, pictureSize.height);
// save width and height in sharedprefrences
width = pictureSize.width;
height = pictureSize.height;
editor.putInt("Picture_Width", width);
editor.putInt("Picture_height", height);
editor.commit();
} else {
// if (pictureSize != null)
parameters.setPictureSize(width, height);
}
}
private Camera.Size getBiggesttPictureSize(Camera.Parameters parameters) {
Camera.Size result = null;
for (Camera.Size size : parameters.getSupportedPictureSizes()) {
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;
if (newArea > resultArea) {
result = size;
}
}
}
return (result);
}
推荐阅读
- ruby - 构建新的 Jekyll 站点时出错
- html - 如何使 Web 部件页面内的 HTML 内容可搜索?
- c# - 如何检查特定父元素的子元素的值?
- go - Go ReadString 问题:文件名、目录名或卷标语法不正确
- pandas - pandas 数据帧上的自定义 word2vec Transformer 并在 FeatureUnion 中使用它
- proxy - 在 oracle 服务总线中使用 MQ 传输时未创建代理服务
- apache-poi - Apache POI XWPFRun 无法获取 EMF 图片
- python - 使用公共键列 pandas 查找任意两列数据帧之间的差异
- angular - 带有嵌套组件的 Angular 2 反应式表单
- go - 如何使用 Go 在终端的一部分中使用 Vim