process image from camera when pressing take a photo button (circle)?

112 Views Asked by At

I did code for processing the image from camera and it works fine, After capturing the image (clicking on take a picture button) it is asking for click ok in camera and then it starts processing image, and what I want is to start processing image immediately after capturing a picture (clicking on take a picture button). I tried with startForActivityResult method but then onActivityResult starts executing after i click on ok button. I also tried with broadcastreceiver but it states that it's depricated way to do this. All in all, I want on pressing take a photo button to take a picture and process it and not after clicking ok button.

b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }
});
1

There are 1 best solutions below

1
fra nic On

Use the new API cameraX ( https://developer.android.com/training/camerax ) It will be more efficient than with camera2basic

With camera2basic : byte[] rgb = new byte[4 * 1920 * 1080]; // contient la trame (frame) reçue, au format RGB 8888 byte[] rgb2 = new byte[4 * 1920 * 1080]; // contient l'image affichée à l'écran après traitement byte[] hls = new byte[4 * 1920 * 1080];

private final TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
        openCamera(1080,1080); //width, height);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) {
        configureTransform(width, height);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) {
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture texture) {

        /*
        texture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
            @Override
            public void onFrameAvailable(SurfaceTexture surfaceTexture) {
                int k=1;
                k++;
                //mGLSurfaceView.requestRender();
            }
        });
        */

        // Get the bitmap

        //
        // surface.invalidate();

        if (mHolder.getSurface().isValid()) {
            final Canvas canvas = mHolder.lockCanvas();
            Log.d("touch", "touchRecieved by camera");
            if (canvas != null) {

                if (config==1)
                {
                    bitmap=mTextureView.getBitmap(1080,1920);
                }
                else
                {
                    bitmap=mTextureView.getBitmap(1080,1920);
                }

                nb_frame++;
                if(nb_frame>5)  // car les premières trames sont noires
                {
                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();

                    int size = bitmap.getRowBytes() * bitmap.getHeight();  
                    ByteBuffer byteBuffer = ByteBuffer.allocate(size);
                    bitmap.copyPixelsToBuffer(byteBuffer);
                    rgb = byteBuffer.array();


                    analyse();  // en entrée : le buffer 'data' (variable globale) au format RGB 8888
                                // en sortie hls -> bitmap_hls
 
                    canvas.drawBitmap(bitmap_hls, 0, 0, paint);
                }
                mHolder.unlockCanvasAndPost(canvas);


            }
        }


    }

};

Use google translate for translating comments