我在我的应用程序中实现了2个方法。第一个,dispatchTakePictureIntent调用startActivityForResult。因此,当我的活动完成后,我回到onActivityResult。
我的应用程序使用事件,所以当我收到特定事件时,我启动相机,所以我可以拍照。问题是:在我开始活动后,如何在不触摸显示屏的情况下拍摄照片,只是从远程设备接收另一个事件?这是我可以打电话给拍照而不是用手指的方法吗?
感谢
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
switch(actionCode) {
case ACTION_TAKE_PHOTO_B:
File f = null;
try {
f = setUpPhotoFile();
mCurrentPhotoPath = f.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
} catch (IOException e) {
e.printStackTrace();
f = null;
mCurrentPhotoPath = null;
}
break;
default:
break;
}
startActivityForResult(takePictureIntent, actionCode);
}
所以我想我必须让系统调用onActivityResult ..
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTION_TAKE_PHOTO_B: {
if (resultCode == RESULT_OK) {
handleBigCameraPhoto();
}
break;
}
default:
break;
}
}
答案 0 :(得分:0)
在我的应用中使用此代码:
public void takePictureNoPreview(Context context){
// open back facing camera by default
Camera myCamera=Camera.open();
if(myCamera!=null){
try{
//set camera parameters if you want to
//...
// here, the unused surface view and holder
SurfaceView dummy=new SurfaceView(context)
myCamera.setPreviewDisplay(dummy.getHolder());
myCamera.startPreview();
myCamera.takePicture(null, null, getJpegCallback()):
}finally{
myCamera.close();
}
}else{
//booo, failed!
}
private PictureCallback getJpegCallback(){
PictureCallback jpeg=new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream fos;
try {
fos = new FileOutputStream("test.jpeg");
fos.write(data);
fos.close();
} catch (IOException e) {
//do something about it
}
}
};
}
}
代码来自here我没有在我的应用中修改它..
答案 1 :(得分:0)
我实现了一个自定义相机,它通过BroadcastReceiver接收Intents。
下面的课程可以通过意图启动,并允许拍照,保存,切换相机(后退/前置)或通过获取意图返回mainActivity。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_preview);
View myView= (View) findViewById(R.id.camera_previeww);
myView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
cameraID= Camera.CameraInfo.CAMERA_FACING_FRONT;
mCamera=openCamera(cameraID);
mCamera.startPreview();
IntentFilter filter = new IntentFilter();
filter.addAction(Tabbed.BROADCAST_ACTION_TABBED);
LocalBroadcastManager bm = LocalBroadcastManager.getInstance(this);
bm.registerReceiver(mBroadcastReceiver, filter);
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) this.findViewById(R.id.camera_previeww);
preview.addView(mPreview);
}
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d(TAG, "Error creating media file, check storage permissions: ");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
sleep(5000);
camera.startPreview();
}
};
private static File getOutputMediaFile(int type){
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "MyCameraApp");
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
}else {
Log.d("MyCameraApp", "mediafile=null");
System.out.println("mediafile=null");
return null;
}
return mediaFile;
}
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
public static final int MEDIA_TYPE_IMAGE = 1;
@Override
protected void onPause() {
super.onPause();
releaseCamera(); // release the camera immediately on pause event
}
private void releaseCamera(){
if (mCamera != null){
mCamera.stopPreview();
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
byte[] data = new byte[3];
if (intent !=null && intent.getAction().equals(Tabbed.BROADCAST_ACTION_TABBED)) {
data = intent.getByteArrayExtra(Tabbed.EXTRA_PARAM_BYTE);
}
if (data[FINGER] == MIDDLE_FINGER && data[TYPE] == SINGLE_TAP) {
switchCamera();
//releaseCamera();
//mCamera=Camera.open();
} else if (data[FINGER] == MIDDLE_FINGER && data[TYPE] == DOUBLE_TAP) {
// HAVE TO GO BACK
onBackPressed();
//onPause();
//finish();
} else if (data[FINGER] == INDEX_FINGER && data[TYPE] == SINGLE_TAP) {
mCamera.takePicture(null, null, mPicture);
}
// kill activity
}
};
/*
@Override
public void onResume(){
super.onResume();
mCamera=openCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);
}
*/
void kill_activity()
{
mCamera.stopPreview();
mCamera.setPreviewCallback(null);
releaseCamera();
finish();
}
public void switchCamera(){
mCamera.stopPreview();
releaseCamera();
if (cameraID==Camera.CameraInfo.CAMERA_FACING_BACK){
cameraID=Camera.CameraInfo.CAMERA_FACING_FRONT;
}else{
cameraID=Camera.CameraInfo.CAMERA_FACING_BACK;
}
mCamera=openCamera(cameraID);
mCamera.startPreview();
CameraPreview mPreview = new CameraPreview(this, mCamera);
preview = (FrameLayout) this.findViewById(R.id.camera_previeww);
preview.removeAllViews();
preview.addView(mPreview);
}
public Camera openCamera(int cameraIDD){
Camera c=null;
try{
c=Camera.open(cameraIDD);
}catch (Exception e){
Log.d("Camera Activity", e.getMessage());
}
return c;
}
}