我不希望我的應用程序調用本機相機應用程序。我想要的只是讓我的應用程序在應用程序啓動時以編程方式拍攝照片,而無需任何用戶交互或參與。我如何使我的相機在android中以編程方式拍照?
這裏是我的代碼:
public class CameraActivity extends Activity implements SurfaceHolder.Callback{
Camera camera;
PictureCallback rawCallback;
ShutterCallback shutterCallback;
PictureCallback jpegCallback;
ImageView view;
boolean inPreview = false;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (ImageView)findViewById(R.id.pic);
Log.e("Camera Status: ",String.valueOf(checkCameraHardware(getApplicationContext())));
Log.e("Number Of Cameras: ",String.valueOf(Camera.getNumberOfCameras()));
start_camera();
captureImage();
rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Log.d("Log", "onPictureTaken - raw");
}
};
/** Handles data for jpeg picture */
shutterCallback = new ShutterCallback() {
public void onShutter() {
Log.i("Log", "onShutter'd");
}
};
jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//Set the Image to ImageView
Bitmap bitmapPicture
= BitmapFactory.decodeByteArray(data, 0, data.length);
//view.setImageBitmap(bitmapPicture);
//Save the Image to SD Card
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(String.format(
"/sdcard/%d.jpg", System.currentTimeMillis()));
outStream.write(data);
outStream.close();
Log.d("Log", "onPictureTaken - wrote bytes: " + data.length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
Log.d("Log", "onPictureTaken - jpeg");
/**
* The two lines below are used to refresh the Surface View
* It works quickly then the general refresh by Default
**/
stop_camera();
start_camera();
}
};
//start_camera();
}
/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private void start_camera()
{
try{
camera = Camera.open();
Log.e("Camera Open Status: ", "Camera opened successfully");
}catch(RuntimeException e){
Log.e("Camera Initialization:", "init_camera: " + e);
return;
}
Camera.Parameters param;
param = camera.getParameters();
//modify parameter
param.setPreviewFrameRate(20);
param.setPreviewSize(176, 144);
camera.setParameters(param);
try {
//camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
//camera.takePicture(shutter, raw, jpeg)
} catch (Exception e) {
Log.e("Camera Initialization:", "init_camera: " + e);
return;
}
inPreview=true;
}
private void captureImage() {
// TODO Auto-generated method stub
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, true);
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
Log.e("Picture Status: ","picture taken successfully");
final Handler handler = new Handler();
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mgr.setStreamMute(AudioManager.STREAM_SYSTEM, false);
}
});
}
}, 1000);
}
private void stop_camera()
{
camera.stopPreview();
camera.release();
inPreview = false;
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
}
究竟什麼是你的問題在此代碼?我已經創建了庫,你可以檢查出來https://github.com/girishnair12345/Girish-Camera-Library-Project –
相機不拍照。另外我得到一個空指針異常。 –
你在哪裏得到NPE? post ur logcat。 – Piyush