How can I make a SurfaceView larger than the screen?

前端 未结 4 930
清酒与你
清酒与你 2021-01-02 00:25

I would like to effectively make a simple digital zoom for the camera preview, so I thought I would simply resize my SurfaceView to be larger than the screen. Other questio

相关标签:
4条回答
  • 2021-01-02 00:52
    //Activity class
    
    public class CameraActivity extends Activity implements SurfaceListener {
    
        private static final String TAG = "CameraActivity";
    
        Camera mCamera;
        CameraPreview mPreview;
        private FrameLayout mCameraPreview;
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
    
            setContentView(R.layout.activity_camera);
    
            mCamera = getCameraInstance();
            mPreview = new CameraPreview(this, mCamera);
    
            mCameraPreview = (FrameLayout) findViewById(R.id.camera_preview);
            mCameraPreview.addView(mPreview);
    
    
        }
    
        @Override
        protected void onPause() {
            super.onPause();
    
            releaseCamera();
        }
    
        private Camera getCameraInstance() {
            Camera camera = null;
            try {
                camera = Camera.open();
            } catch (Exception e) {
    
            }
            return camera;
        }
    
        private void releaseCamera() {
            if (null != mCamera) {
                mCamera.release();
            }
    
            mCamera = null;
        }
    
        @Override
        public void surfaceCreated() {
    
            //Change these mate
            int width = 1000;
            int height = 1000;
            // Set parent window params
            getWindow().setLayout(width, height);
    
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    width, height);
            mCameraPreview.setLayoutParams(params);
            mCameraPreview.requestLayout();
        }
    }
    
    // Preview class
    
    public class CameraPreview extends SurfaceView implements
            SurfaceHolder.Callback {
    
        private static final String TAG = "CameraPreview";
    
        Context mContext;
        Camera mCamera;
        SurfaceHolder mHolder;
    
        public interface SurfaceListener{
            public void surfaceCreated();
        }
        SurfaceListener listener;
    
    
        public CameraPreview(Context context, Camera camera) {
            super(context);
    
            mContext = context;
            listener = (SurfaceListener)mContext;
            mCamera = camera;
    
            mHolder = getHolder();
            mHolder.addCallback(this);
    
            // Required prior 3.0 HC
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
    
            try {
                mCamera.setPreviewDisplay(holder);
    
                Parameters params = mCamera.getParameters();
                //Change parameters here
                mCamera.setParameters(params);
    
                mCamera.startPreview();
    
                listener.surfaceCreated();
    
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    
        public void surfaceDestroyed(SurfaceHolder holder) {
            // empty. Take care of releasing the Camera preview in your activity.
        }
    
        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            // If your preview can change or rotate, take care of those events here.
            // Make sure to stop the preview before resizing or reformatting it.
    
            Log.i(TAG, "Surface changed called");
            if (mHolder.getSurface() == null) {
                // preview surface does not exist
                return;
            }
    
            // stop preview before making changes
            try {
                mCamera.stopPreview();
            } catch (Exception e) {
                // ignore: tried to stop a non-existent preview
            }
    
            // set preview size and make any resize, rotate or
            // reformatting changes here
            mCamera.setDisplayOrientation(90);
    
            // start preview with new settings
            try {
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
    
            } catch (Exception e) {
                Log.d(TAG, "Error starting camera preview: " + e.getMessage());
            }
        }
    
    }
    
    //Layout file 
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <FrameLayout
            android:id="@+id/camera_preview"
            android:layout_width="300dp"
            android:layout_height="400dp"
            android:layout_centerHorizontal="true"
            android:paddingTop="10dp" >
        </FrameLayout>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2021-01-02 00:57

    Here's my TouchSurfaceView's onMeasure that performs zoom:

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);
            setMeasuredDimension((int) (width * scaleFactor), (int) (height * scaleFactor));
        }
    

    This properly zooms in and out depending on scaleFactor.

    I haven't tested this with camera, but it works properly with MediaPlayer (behaving as VideoView).

    0 讨论(0)
  • 2021-01-02 01:06

    What you can do is to get the window and set its height:

    getWindow().setLayout(1000, 1000);
    

    This makes your window larger than the screen making your root view and consequently your surfaceview, probably contained inside a Framelayout larger than screen.

    This worked for me let me know.

    The above would work no matter what. What you would want to do is listen for onSurfaceCreated event for your surface view. Then after you have the started the camera view and you are able to calculate size of your widget holding the preview, you would want to change size of the container widget.

    The concept is your container widget (probably FrameLayout) wants to grow larger than screen. The screen itself is restricted by the activity so first set size of your window,

    then set size of your framelayout (it would always be shrunk to max size of windows, so set accordingly).

    I do all this logic after my onSurfaceCreated is finished I have started the preview. I listen for this event in my activity by implementing a small interface, as my Camera preview is a separate class.

    Working on all API level >= 8

    0 讨论(0)
  • 2021-01-02 01:16

    You can't make your surfaceView bigger than the screen. That being said there are ways around it.

    I found you can adjust the size of the canvas in the SurfaceView, which will allow zooming.

    public class DrawingThread extends Thread {
    private MagnificationView mainPanel;
    private SurfaceHolder surfaceHolder;
    private boolean run;
    
    public DrawingThread(SurfaceHolder surface, MagnificationView panel){
        surfaceHolder = surface;
        mainPanel = panel;
    }
    
    public SurfaceHolder getSurfaceHolder(){
        return surfaceHolder;
    }
    
    public void setRunning (boolean run){
        this.run = run;
    }
    
    public void run(){
        Canvas c;
        while (run){
            c = null;
            try {
                c = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder){
                    mainPanel.OnDraw(c);
                }
            } finally {
                if (c != null){
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
    

    }

    In the MagnificationView class add a method:

    public void OnDraw(Canvas canvas){
        if (canvas!=null){
            canvas.save();
            canvas.scale(scaleX,scaleY);      
    
            canvas.restore();
        }
    }
    

    DrawingThread would be a thread you start in in your Activity. Also in your MagnificationView class override the OnTouchEvent to handle your own pinch-zoom (which will modify scaleX & scaleY.

    Hope This solves your issue

    0 讨论(0)
提交回复
热议问题