Hi .... Dear All .... Today I am uploading a superb example of how Image render on Surface.
This concept is implemented by extending SurfaceView Class and implementing SurfaceHolder Callback Methods.
We have to use Camera of the device so add the below permission in manifest file
Target SDK API 23
Please Like us & put your valuable suggestions in comment box
This concept is implemented by extending SurfaceView Class and implementing SurfaceHolder Callback Methods.
We have to use Camera of the device so add the below permission in manifest file
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
SurfaceView
@ Provides a dedicated drawing surface embedded inside of a view hierarchy.
@ One of the purposes of this class is to provide a surface in which a secondary thread can
render into the screen.
@ One of the purposes of this class is to provide a surface in which a secondary thread can
render into the screen.
I am using Android Studio 1.5.1Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box
B. Java Code
1. MainActivity.java will be like
package com.exam.ravi.surfaceviewex1; import android.hardware.Camera; import android.hardware.Camera.ErrorCallback; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; @SuppressWarnings("ALL") public class MainActivity extends AppCompatActivity { private static final String TAG = "RenderDemo"; private RenderPreview renderPreview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Camera camera = Camera.open(); renderPreview = new RenderPreview(this, camera); renderPreview.camera.setErrorCallback(new ErrorCallback() { @Override public void onError(int error, Camera camera) { System.out.println("Error in camera -- " + error); } }); setContentView(renderPreview); } }
2. RenderPreview.java is like
package com.exam.ravi.surfaceviewex1; import android.content.Context; import android.hardware.Camera; import android.hardware.Camera.PreviewCallback; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import java.io.IOException; @SuppressWarnings("ALL") class RenderPreview extends SurfaceView implements SurfaceHolder.Callback { private static final String TAG = "RenderPreview"; SurfaceHolder surfaceHolder; public Camera camera; public byte[] cameraData; RenderPreview(Context context, Camera camera) { super(context); this.camera = camera; surfaceHolder = getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } // Called once the holder is readypublic void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, acquire the camera and tell it where to draw.try { camera.setPreviewDisplay(holder); camera.setPreviewCallback(new PreviewCallback() { // Called for each frame previewedpublic void onPreviewFrame(byte[] data, Camera camera) { cameraData = data; Log.d(TAG, "Size of camera Data - " + cameraData.length); RenderPreview.this.invalidate(); } }); } catch (IOException e) { e.printStackTrace(); } } // Called when surface holder is destroyed
public void surfaceDestroyed(SurfaceHolder holder) { holder.removeCallback(this); } // Called when surface holder has changed
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { try { camera.startPreview(); } catch (RuntimeException e) { Log.e("CameraPreview", "Runtime Exeption " + e.getLocalizedMessage()); } } }
No comments:
Post a Comment