[英]Take picture with front camera in android service
I need to take a picture with the back camera in an android service but after reading the docs it seems you need a SurfaceView
is it possible to take a picture without showing anything to the user? 我需要在android服务中使用后置摄像头拍照,但是在阅读文档后,似乎您需要SurfaceView
是否可以在不向用户显示任何东西的情况下拍照?
Edit: Will this work? 编辑:这行得通吗?
SurfaceTexture surfaceTexture = new SurfaceTexture(10);
Camera camera = Camera.open();
camera.getParameters().setPreviewSize(1, 1);
camera.setPreviewTexture(surfaceTexture);
camera.startPreview();
camera.takePicture(null, pictureCallback, null);
100% working . 100%工作。 click picture from front camera using service . 使用服务从前置摄像头单击图片。
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
CapturePhoto();
}
private void CapturePhoto() {
Log.d("kkkk","Preparing to take photo");
Camera camera = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int frontCamera = 1;
//int backCamera=0;
Camera.getCameraInfo(frontCamera, cameraInfo);
try {
camera = Camera.open(frontCamera);
} catch (RuntimeException e) {
Log.d("kkkk","Camera not available: " + 1);
camera = null;
//e.printStackTrace();
}
try {
if (null == camera) {
Log.d("kkkk","Could not get camera instance");
} else {
Log.d("kkkk","Got the camera, creating the dummy surface texture");
try {
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
} catch (Exception e) {
Log.d("kkkk","Could not set the surface preview texture");
e.printStackTrace();
}
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir=new File("/sdcard/CaptureByService");
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
pictureFileDir.mkdirs();
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "ServiceClickedPic_" + "_" + date + ".jpg";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
File mainPicture = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(mainPicture);
fos.write(data);
fos.close();
Log.d("kkkk","image saved");
} catch (Exception error) {
Log.d("kkkk","Image could not be saved");
}
camera.release();
}
});
}
} catch (Exception e) {
camera.release();
}
}
}
You can set Width
and Height
of SurfaceView
to 1dp
and margintop
to - 10
,so that it wont display it on screen but it functions as normal and you can Take picture without displaying Surface View to User 您可以将SurfaceView
Width
和Height
设置为1dp
, margintop
为10
,这样它就不会在屏幕上显示它,但是它的功能正常,并且可以拍摄照片而不会向用户显示Surface View
<SurfaceView
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_marginTop="-10dp"
...
/>
in that case you can just use SurfaceTexture
在这种情况下,您可以只使用SurfaceTexture
SurfaceTexture surfaceTexture = new SurfaceTexture(10);
Camera camera = Camera.open();
camera.getParameters().setPreviewSize(1, 1);
camera.setPreviewTexture(surfaceTexture);
Yes it is possible. 对的,这是可能的。 call your Camera Callback method to take picture from camera and Dont specify the size for preview and just start the preview it will work . 调用您的Camera Callback方法从相机拍摄照片, 不要指定预览的大小,只需启动预览即可使用。
Omit this Step : 忽略此步骤:
param.setPreviewSize(122,133);
or use 或使用
param.setPreviewSize(1, 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.