Good day all, I am trying to figure out how to take a picture by pressing a button, without any preview showing up. The idea is, I want a picture to be taken and saved, but no visual preview of the photo before or afterwards. So far, I am able to get the code to take pictures and save them to the disk without any problems, but I cannot seem to do it without a surfaceview or preview.
Here is some of my code:
Main Activity:
public class MainActivity extends Activity implements OnClickListener {
private Camera cameraObject;
private ShowCamera showCamera;
private Button NOPE;
//Check if camera is avail:
public static Camera isCameraAvailiable(){
Camera object = null;
try {
object = Camera.open();
L.m("Camera Open");
} catch (Exception e) {
L.m(e.toString());
} return object;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Opens up the camera
cameraObject = isCameraAvailiable();
//Sets the resolution for the camera (Excluded from code here)
setCameraResolution();
//Button for taking photos
NOPE = (Button) findViewById(R.id.button_capture);
NOPE.setOnClickListener(this);
//THIS SECTION OF CODE HERE I can't get it to work without it as this creates a view/ preview for the camera
showCamera = new ShowCamera(this, cameraObject);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
public void snapIt(View view){
cameraObject.takePicture(null, null, new PhotoHandler(getApplicationContext()));
}
public void onClick(View view) {
switch (view.getId()){
case R.id.button_capture:
snapIt(view);
}
}
}
The photo handler class:
public class PhotoHandler implements PictureCallback {
private final Context context;
public PhotoHandler(Context context) {
this.context = context;
}
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFileDir = getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
//I removed unnecessary code here, but this is where I write to disk, which works fine.
}
}
The problem I am having is that I CANNOT actually take a photo via the camera unless I have the code about preview.addView(showCamera);. The ShowCamera class is merely one that adds a surface view for viewing the pictures while they are being taken:
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback
Anyone have any ideas? Can this be done?
Someone asked an EXTREMELY similar question here: Taking picture without SurfaceView or without Photo Intent Activity , but without any success. I think I'm on a similar path as they were.
I have the same problem and I solve it by puting new view over the SurficeView so surficeView was not seen. I spend quite a time by searching for other solution but without success.