1

I am new to cordova. working on ble device project.

My requirement is,

  • First click on ble device should open the camera.
  • next corresponding clicks to take photos .

AutomaticPhotoActivity.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;

public class AutomaticPhotoActivity extends Activity{
    private Camera camera; // camera object
    private TextView textTimeLeft; // time left field


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textTimeLeft=(TextView)findViewById(R.id.textTimeLeft); // make time left object

      /*  Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 0);*/
            camera = Camera.open(0);
        try {
            SurfaceView view = new SurfaceView(this);


            camera.setPreviewDisplay(view.getHolder()); // feed dummy surface to surface
            SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE);
            camera.setPreviewTexture(st);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Camera.PictureCallback jpegCallBack=new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            // set file destination and file name
            File destination=new File(Environment.getExternalStorageDirectory(),"myPicture.jpg");
            try {
                Bitmap userImage = BitmapFactory.decodeByteArray(data, 0, data.length);
                // set file out stream
                FileOutputStream out = new FileOutputStream(destination);
                // set compress format quality and stream
                userImage.compress(Bitmap.CompressFormat.JPEG, 90, out);

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    };
//  onclick off button wait for 5seconds then take phot
    public void startTimer(View v){

        // 5000ms=5s at intervals of 1000ms=1s so that means it lasts 5 seconds
        new CountDownTimer(5000,1000){

            @Override
            public void onFinish() {
                // count finished
                textTimeLeft.setText("Picture Taken");
                camera.takePicture(null, null, null, jpegCallBack);
            }

            @Override
            public void onTick(long millisUntilFinished) {
                // every time 1 second passes`enter code here`
                textTimeLeft.setText("Seconds Left: "+millisUntilFinished/1000);
            }

        }.start();
    }

}

I can take a photo using this code but without default camera opened. How to take a photo after opening the camera without using surfaceview.As i am doing all my designs in html css,no xml. please help me.

UPDATE

Those two answer was using surface view.In my case i need to open default camera app and has to take a photo. Like how selfie stick works.

in simple: how to automatically take photo from default camera app.

sachin.g
  • 31
  • 8
  • No this question is different than those answers.I want to take a photo from default camera app without user interaction. – sachin.g Jul 18 '16 at 12:56

1 Answers1

-1

Try this code.It will launch default camera and you will get callback in on activity result after click on save pic button.

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

REQUEST_IMAGE_CAPTURE will any int value.

https://developer.android.com/training/camera/photobasics.html

Suhas 007
  • 56
  • 1
  • 8
  • 1
    thanks for the reply.but this one i tried already it just launches the default camera app but wont take photo automatically. – sachin.g Jul 18 '16 at 12:10