Android Open Source - sony-camera-remote-java Live View






From Project

Back to project page sony-camera-remote-java.

License

The source code is released under:

Apache License

If you think the Android project sony-camera-remote-java listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.praetoriandroid.cameraremote.app;
//  w  ww  .j a v a  2  s  .  c o m
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import org.androidannotations.annotations.EView;
import org.androidannotations.annotations.UiThread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

@EView
public class LiveView extends SurfaceView implements SurfaceHolder.Callback {

    private static class BitmapHolder {
        private Bitmap bitmap;

        public BitmapHolder(Bitmap bitmap) {
            this.bitmap = bitmap;
        }

        public Bitmap getBitmap() {
            return bitmap;
        }
    }

    private static class LiveViewThread extends Thread {

        private static final BitmapHolder STOP_MARKER = new BitmapHolder(null);

        private final SurfaceHolder surfaceHolder;
        private final BlockingQueue<BitmapHolder> frameQueue = new LinkedBlockingQueue<BitmapHolder>(2);
        private volatile boolean run;
        private Rect viewRect = new Rect();
        private int canvasWidth = 1;
        private int canvasHeight = 1;

        public LiveViewThread(SurfaceHolder surfaceHolder) {
            this.surfaceHolder = surfaceHolder;
        }

        public void startShow() {
            synchronized (surfaceHolder) {
                run = true;
                start();
            }
        }

        public void stopShow() {
            synchronized (surfaceHolder) {
                frameQueue.clear();
                if (!frameQueue.offer(STOP_MARKER)) {
                    if (BuildConfig.DEBUG) {
                        throw new IllegalStateException();
                    }
                    interrupt();
                }
                run = false;
            }
            boolean retry = true;
            while (retry) {
                try {
                    join();
                    retry = false;
                } catch (InterruptedException ignored) {
                }
            }
        }

        public void putFrame(Bitmap frame) {
            synchronized (surfaceHolder) {
                frameQueue.offer(new BitmapHolder(frame));
            }
        }

        @Override
        public void run() {
            while (run) {
                Canvas canvas = null;
                try {
                    BitmapHolder frameHolder = frameQueue.take();
                    if (frameHolder == STOP_MARKER) {
                        return;
                    }
                    canvas = surfaceHolder.lockCanvas();
                    synchronized (surfaceHolder) {
                        if (run) {
                            drawFrame(canvas, frameHolder.getBitmap());
                        }
                    }
                } catch (InterruptedException e) {
                    interrupt();
                    return;
                } finally {
                    if (canvas != null) {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
            }
        }

        private void drawFrame(Canvas c, Bitmap frame) {
            float frameWidth = frame.getWidth();
            float frameHeight = frame.getHeight();
            float scaleFactor = Math.min(canvasWidth / frameWidth, canvasHeight / frameHeight);
            int viewportWidth = Math.round(scaleFactor * frameWidth);
            int viewportHeight = Math.round(scaleFactor * frameHeight);

            viewRect.left = (canvasWidth - viewportWidth) / 2;
            viewRect.top = (canvasHeight - viewportHeight) / 2;
            viewRect.right = viewRect.left + viewportWidth;
            viewRect.bottom = viewRect.top + viewportHeight;

            c.drawBitmap(frame, null, viewRect, null);
        }

        public void setSurfaceSize(int width, int height) {
            synchronized (surfaceHolder) {
                canvasWidth = width;
                canvasHeight = height;
            }
        }
    }

    private volatile LiveViewThread liveViewThread;

    public LiveView(Context context) {
        this(context, null, 0);
    }

    public LiveView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LiveView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        SurfaceHolder surfaceHolder = getHolder();
        assert surfaceHolder != null;
        surfaceHolder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        liveViewThread = new LiveViewThread(holder);
        liveViewThread.startShow();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        liveViewThread.stopShow();
        liveViewThread = null;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        liveViewThread.setSurfaceSize(width, height);
    }

    public void putFrame(Bitmap frame) {
        LiveViewThread thread = liveViewThread;
        if (thread != null) {
            thread.putFrame(frame);
        }
    }

}




Java Source Code List

com.praetoriandroid.cameraremote.BadHttpStatusException.java
com.praetoriandroid.cameraremote.DeviceDescription.java
com.praetoriandroid.cameraremote.HttpClient.java
com.praetoriandroid.cameraremote.InvalidDataFormatException.java
com.praetoriandroid.cameraremote.LiveViewDisconnectedException.java
com.praetoriandroid.cameraremote.LiveViewFetcher.java
com.praetoriandroid.cameraremote.Logger.java
com.praetoriandroid.cameraremote.ParseException.java
com.praetoriandroid.cameraremote.RpcClient.java
com.praetoriandroid.cameraremote.RpcException.java
com.praetoriandroid.cameraremote.ServiceNotSupportedException.java
com.praetoriandroid.cameraremote.SsdpClient.java
com.praetoriandroid.cameraremote.SsdpException.java
com.praetoriandroid.cameraremote.app.LiveView.java
com.praetoriandroid.cameraremote.app.MainActivity.java
com.praetoriandroid.cameraremote.app.Rpc.java
com.praetoriandroid.cameraremote.app.SelfTimerSelector.java
com.praetoriandroid.cameraremote.app.ThisApp.java
com.praetoriandroid.cameraremote.rpc.ActTakePictureRequest.java
com.praetoriandroid.cameraremote.rpc.ActTakePictureResponse.java
com.praetoriandroid.cameraremote.rpc.AwaitTakePictureRequest.java
com.praetoriandroid.cameraremote.rpc.BaseRequest.java
com.praetoriandroid.cameraremote.rpc.BaseResponse.java
com.praetoriandroid.cameraremote.rpc.CustomizableEntity.java
com.praetoriandroid.cameraremote.rpc.EventEntity.java
com.praetoriandroid.cameraremote.rpc.GetApplicationInfoRequest.java
com.praetoriandroid.cameraremote.rpc.GetApplicationInfoResponse.java
com.praetoriandroid.cameraremote.rpc.GetAvailableApiListRequest.java
com.praetoriandroid.cameraremote.rpc.GetAvailableApiListResponse.java
com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetAvailableSelfTimerResponse.java
com.praetoriandroid.cameraremote.rpc.GetEventRequest.java
com.praetoriandroid.cameraremote.rpc.GetEventResponse.java
com.praetoriandroid.cameraremote.rpc.GetExposureModeRequest.java
com.praetoriandroid.cameraremote.rpc.GetMethodTypesRequest.java
com.praetoriandroid.cameraremote.rpc.GetMethodTypesResponse.java
com.praetoriandroid.cameraremote.rpc.GetSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetSupportedSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.GetSupportedSelfTimerResponse.java
com.praetoriandroid.cameraremote.rpc.GetVersionsRequest.java
com.praetoriandroid.cameraremote.rpc.GetVersionsResponse.java
com.praetoriandroid.cameraremote.rpc.IllegalResultSizeException.java
com.praetoriandroid.cameraremote.rpc.MethodTypesEntity.java
com.praetoriandroid.cameraremote.rpc.RpcMethod.java
com.praetoriandroid.cameraremote.rpc.SetSelfTimerRequest.java
com.praetoriandroid.cameraremote.rpc.SimpleResponse.java
com.praetoriandroid.cameraremote.rpc.StartLiveViewRequest.java
com.praetoriandroid.cameraremote.rpc.StartLiveViewResponse.java
com.praetoriandroid.cameraremote.rpc.StartMovieRecRequest.java
com.praetoriandroid.cameraremote.rpc.StartRecModeRequest.java
com.praetoriandroid.cameraremote.rpc.StopLiveviewRequest.java
com.praetoriandroid.cameraremote.rpc.StopMovieRecRequest.java
com.praetoriandroid.cameraremote.rpc.StopMovieRecResponse.java
com.praetoriandroid.cameraremote.rpc.StopRecModeRequest.java
com.praetoriandroid.cameraremote.rpc.ValidationException.java
com.praetoriandroid.cameraremote.rpc._DummyResponse.java
com.praetoriandroid.cameraremote.tool.Cache.java
com.praetoriandroid.cameraremote.tool.RemoteControl.java
com.praetoriandroid.widget.ItemViewCreator.java
com.praetoriandroid.widget.RadialFreeSpace.java
com.praetoriandroid.widget.RadialSelector.java