Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.hardware.Camera;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.ViewGroup;
- import android.webkit.WebView;
- import com.MyCustomContext;
- public class CameraPluginView extends ViewGroup implements SurfaceHolder.Callback {
- private final String TAG = "camera.CameraPluginView";
- MyCustomContext customContext;
- SurfaceView surfaceView;
- public WebView overlayWebView;
- Camera camera;
- public CameraPluginView(final MyCustomContext customContext){
- super(customContext);
- this.setBackgroundColor(Color.WHITE);
- this.customContext = customContext;
- this.surfaceView = new SurfaceView(customContext);
- this.surfaceView.getHolder().addCallback(this);
- this.addView(this.surfaceView);
- context.getActivity().runOnUiThread(new Runnable() {
- @SuppressLint("SetJavaScriptEnabled")
- public void run(){
- addView(surfaceView);
- overlayWebView = new OverlayWebView(customContext);
- overlayWebView.getSettings().setJavaScriptEnabled(true);
- overlayWebView.getSettings().setDatabaseEnabled(true);
- overlayWebView.addJavascriptInterface(
- new Object() {
- @SuppressWarnings("unused")
- public void callParentView(final String js){
- context.getActivity().runOnUiThread(new Runnable(){
- public void run(){
- context.loadUrl("javascript:" + js);
- }
- });
- }
- },
- "root"
- );
- overlayWebView.setBackgroundColor(0 << 24);
- overlayWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
- addView(overlayWebView);
- }
- });
- }
- public CameraPluginView(Context context){
- super(context);
- }
- public void setCamera(Camera camera){
- this.camera = camera;
- if(camera != null){
- try {
- this.camera.setPreviewDisplay(this.surfaceView.getHolder());
- this.camera.startPreview();
- } catch (IOException e) {
- e.printStackTrace();
- }
- this.requestLayout();
- }
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- if(this.camera != null){
- this.setCamera(this.camera);
- }
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- if(this.camera != null){
- this.setCamera(this.camera);
- }
- }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b){
- int width = r - l;
- int height = b - t;
- this.getChildAt(0).layout(0, 0, width, height);
- if(this.getChildCount() > 1){
- this.getChildAt(1).layout(0, 0, width, height);
- }
- }
- }
- class OverlayWebView extends WebView {
- public OverlayWebView(Context context) {
- super(context);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- invalidate(); // Invalidate all the time so there is no lag on display.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment