Android create camera preview frames

I want to make an Android application that uses a camera and applies image processing filters in preview frames.

package alex.filter; import java.io.IOException; import android.content.Context; import android.graphics.Canvas; import android.hardware.Camera; import android.hardware.Camera.PreviewCallback; import android.view.SurfaceHolder; import android.view.SurfaceView; class Preview extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder mHolder; public Camera camera; Preview(Context context) { super(context); mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); try { camera.setPreviewDisplay(holder); camera.setPreviewCallback(new PreviewCallback() { public void onPreviewFrame(byte[] data, Camera arg1) { for( int i = 0 ; i < data.length ; i ++ ){ data[ i] = 0; // or some sirius filter } Preview.this.invalidate(); } }); } catch (IOException e) { e.printStackTrace(); } } public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera = null; } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Camera.Parameters parameters = camera.getParameters(); parameters.setPreviewSize(w, h); camera.setParameters(parameters); camera.startPreview(); } @Override public void draw(Canvas canvas) { super.draw(canvas); } } 

However, I see no changes in the emulator no matter what I do in the onPreviewFrame method.

+6
source share
3 answers

Another option is to use the OpenCV infrastructure, which has an Android port:

http://opencv.willowgarage.com/wiki/Android2.3.0

This is the open source NDK port of the Open Open Vision project, and it will use raw preview frames and allow them to be processed using OpenCV before displaying them on SurfaceView. Since it manages frames, it does not work at the same frame rate as simply associated with hardware-optimized previews, but since it is very popular, it does a pretty good job.

There is an OpenCV_Sample application in the above version, which is compiled into a demo application that can do a lot of what you are looking for. It has menu options to enable invert, blur, or edge detection in the preview area. Even if this is not exactly what you want, there are some excellent samples in the source code.

+4
source

See this link , I think it looks like what you want to achieve.

+2
source

Good, because the preview buffers that you receive in the callback are only a copy of the preview buffers, so any changes you make will not be displayed, since the buffer you receive is your copy. Mentioned in android sdk here

I'm not sure how to do this, but I was thinking about how to do it, and here is what I have to do -

  • Register Preview Buffers
  • Disable default preview display
    • If you don’t install the preview screen on the surface, you should not display (but I'm not sure that it will work) by reading it on some forum that the source couldn’t remember)
    • If the above does not work, we will need to hide the view (I know that it’s hardly effective, but I could not come up with anything else).
  • Reduce the preview frame rate so that we are not overloaded with buffers.
  • Now, to display our buffers, we can either use the default bitmap drawing function, or use opengl to display buffers. But so far I have not used either one or the other, I do not even know if this is possible, any thoughts on the same thing?

UPDATE
Revising the SDK documentation I found this API - setPreviewTexture , this API allows us to "capture frames from an image stream as an OpenGL ES texture." When you have images with texture applied, you can use OpenGL to display your frames. (Take a look at the answer posted by @Stephan on how to do this.)

NOTE - setPreviewTexture is available from API level starting at 11! SDK Link

+1
source

Source: https://habr.com/ru/post/891232/


All Articles