Search code examples
androidcameraphoto

unable to take picture with camerakit


I'm following this Documentation http://docs.camerakit.website/#/ I'm trying to take a photo but I can't find the exact code. When I write this code in my onCreate method android studio says to me that is not a correct code:

camera.setCameraListener(new CameraListener() {
    @Override
    public void onPictureTaken(byte[] picture) {
        super.onPictureTaken(picture);

        // Create a bitmap    
        Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
    }
 });

camera.captureImage();

Here is my entire code:

public class MainActivity extends AppCompatActivity {

    CameraView cameraView;
    ImageView img_photo;
    Bitmap photo;
    Button btt_scatta;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        //Get root view from Activity
        final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);

        cameraView = (CameraView) findViewById(R.id.camera);
        cameraView.setFacing(CameraKit.Constants.FACING_FRONT);
        btt_scatta = (Button) findViewById(R.id.btt_scatta);

        img_photo = (ImageView) findViewById(R.id.img_photo);

        camera.setCameraListener(new CameraListener() {
        @Override
        public void onPictureTaken(byte[] picture) {
            super.onPictureTaken(picture);

            // Create a bitmap    
            Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
        }
     });

        btt_scatta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                photo = getScreenShot(rootView);
                img_photo.setImageBitmap(photo);
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        cameraView.start();
    }

    @Override
    protected void onPause() {
        cameraView.stop();
        super.onPause();
    }

    //Capture the root view
    public static Bitmap getScreenShot(View view) {
        View screenView = view.getRootView();
        screenView.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
        screenView.setDrawingCacheEnabled(false);
        return bitmap;
    }

    //Store the Bitmap into the phone
    public static void store(Bitmap bm, String fileName){
        final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
        File dir = new File(dirPath);
        if(!dir.exists())
            dir.mkdirs();
        File file = new File(dirPath, fileName);
        try {
            FileOutputStream fOut = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
            fOut.flush();
            fOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

I've already followed the entire code without any results. You can find the code at github website: https://github.com/CameraKit/camerakit-android My scope is to create an app that take a photo from you camera and then show you the picture in an Imageview. I want to say that the camera works fine with this code. I don't have any problem with the camera, but only in the moment to take a picture. Thanks to everybody.


Solution

  • You should call

    camera.captureImage()
    

    inside the click listener of your btt_scatta.