0

Is there any way to get photo without preview and put it to database in android sdk 19 In the below code automaticly getting location on every 5 minutes and putting marker on map additionally i need to take photo automaticly and put it to database

    public void startCountDown() {
    if (this._countDownTimer != null) {
        this._countDownTimer.cancel();
    }
    _countDownTimer = new CountDownTimer(60000 * 5, 60000*5) {
        @Override
        public void onFinish() {

        }
        @Override
        public void onTick(long millisUntilFinished) {
            putMarkerToLocation();
            takePhotoAndSaveIt2Db(); // ==> here what i need
        }
    }.start();
}
hkn
  • 371
  • 1
  • 14
  • What do you mean with "without showing preview"? You can just store the path of the image without viewing it.. – HB. Dec 16 '17 at 17:09
  • this project is for taxi , i will get location of taxi, and customers photo to db, this operations will be done when taximeter starts automaticly without human control – hkn Dec 16 '17 at 17:21
  • Ok and what do you want help with? – HB. Dec 16 '17 at 18:21
  • i know java but dont know android is it posible to take photo + get location and put them to database with functions under CountDownTimer if not i will try to find another strategy – hkn Dec 16 '17 at 18:46
  • Yes it is definitely possible. Once the image is taken you can temporarily store it to the device, then upload it to the database, when successfully stored in database you can delete it from the device. The location is simple, once the image is taken you get the location and store it and it will work perfectly when using a CountdownTimer – HB. Dec 16 '17 at 18:59
  • hımm i got rid off the location thing btw just camera left i think i need to take permissions then take photo – hkn Dec 16 '17 at 20:33
  • https://stackoverflow.com/questions/20172409/android-take-photo-without-user-interface i tried solution here CapPhoto service but i isnt working because of surfaceholder or android does not allow to take photo without users knowledge – hkn Dec 16 '17 at 22:12

1 Answers1

0

Below service works percfectly on api19

public class CapPhoto extends Service {
private Camera mCamera;

@Override
public void onCreate() {
    super.onCreate();
    Log.d("CAM", "start");

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy =
                new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    Thread myThread = null;


}

@Override
public void onStart(Intent intent, int startId) {
    takePhoto();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void takePhoto() {

    System.out.println("Fotoraf Cekimi Hazirligi Basladi");
    Camera camera = null;

    int cameraCount = 0;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        SystemClock.sleep(1000);

        Camera.getCameraInfo(camIdx, cameraInfo);

        try {
            camera = Camera.open(camIdx);
        } catch (RuntimeException e) {
            System.out.println("Camera not available: " + camIdx);
            camera = null;
            //e.printStackTrace();
        }
        try {
            if (null == camera) {
                System.out.println("Could not get camera instance");
            } else {
                System.out.println("Got the camera, creating the dummy surface texture");
                //SurfaceTexture dummySurfaceTextureF = new SurfaceTexture(0);
                try {
                    //camera.setPreviewTexture(dummySurfaceTextureF);
                    camera.setPreviewTexture(new SurfaceTexture(0));
                    camera.startPreview();
                } catch (Exception e) {
                    System.out.println("Could not set the surface preview texture");
                    e.printStackTrace();
                }
                camIdx = cameraCount;

                Camera.Parameters params = camera.getParameters();
                params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
                params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
                params.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);
                params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
                params.setExposureCompensation(0);
                params.setPictureFormat(ImageFormat.JPEG);
                params.setJpegQuality(100);
                params.setRotation(90);
                camera.setParameters(params);

                camera.takePicture(null, null, new Camera.PictureCallback() {

                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {
                        File pictureFileDir =new File(Environment.getExternalStorageDirectory(), "A");
                        if(!pictureFileDir.exists()){
                            pictureFileDir.mkdirs();
                        } //                            File pictureFileDir = getDir();
                        if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
                            return;
                        }
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
                        String date = dateFormat.format(new Date());
                        String photoFile = "TaksiResim_" + "_" + date + ".jpg";
                        String filename = pictureFileDir.getPath() + File.separator + photoFile;
                        File mainPicture = new File(filename);
                        //addImageFile(mainPicture);

                        try {
                            FileOutputStream fos = new FileOutputStream(mainPicture);
                            fos.write(data);
                            fos.close();
                            System.out.println("resim kayit edildi");
                        } catch (Exception error) {
                            System.out.println("resim kayit edilemedi");
                        }
                        camera.release();
                    }
                });
            }
        } catch (Exception e) {
            camera.release();
        }
    }
} }
hkn
  • 371
  • 1
  • 14