我开发了一个Android应用程序。因为我使用了前置摄像头功能。它工作正常,但我需要自动捕获。即没有点击快门按钮的声音,我想拍摄光电镜..
我的相机活动代码
private Camera openFrontFacingCameraGingerbread() {
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.i("Camera failed to open: ",e.getLocalizedMessage());
}
}
}
return cam;
}
提前致谢..
答案 0 :(得分:5)
按照Android开发人员reference pages中列出的步骤操作。没有要求“快门按钮”。如果您不想在屏幕上显示图像,则可以创建虚拟SurfaceHolder,例如
SurfaceView surface = new SurfaceView(context);
cam.setPreviewDisplay(surface.getHolder());
答案 1 :(得分:1)
public int intPicTaken;
// setPreviewCallback on the camera, wait intil intPicTaken increments to 10, then take the picture
cam.setPreviewCallback(prevCallBack);
public Camera.PreviewCallback prevCallBack = new Camera.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
intPicTaken++;
try {
if(intPicTaken == 10) {
doTakePicture();
}
} catch (Exception e) {
System.out.println("onPreviewFrame: " + e.toString());
}
}
};
public Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
System.out.println("PictureCallback onPictureTaken");
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length, options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
picture.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.close();
System.out.println("PictureCallback onPictureTaken done");
cam.release();
saveFile(picture);
} catch (Exception e) {
System.out.println("onPictureTaken: " + e.toString());
}
}
};
// take the picture
public void doTakePicture() {
try {
cam.stopPreview();
cam.takePicture(null, null, mPicture, mPicture);
} catch(Exception e){
System.out.println("doTakePicture: " + e.toString());
}
}
// saving the file to gallery
public void saveFile(Bitmap bitmap) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaStorageDir = Environment.getExternalStorageDirectory();
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
System.out.println("saveFile: failed to create directory");
return;
}
}
try {
String saved = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "title", "description");
Uri sdCardUri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, sdCardUri));
System.out.println("file saved");
} catch (Exception e) {
System.out.println("saveFile: " + e.toString());
e.printStackTrace();
}
}
答案 2 :(得分:1)
将此代码写入“创建自动捕获图像”
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom__camera_activity);
mCamera = getCameraInstance();
mCameraPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
{
mCamera.takePicture(null, null, mPicture);
}
}
}, 5500);
}