Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7909599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:29:34+00:00 2026-06-03T12:29:34+00:00

I am trying to create a surface view for a camera so it renders

  • 0

I am trying to create a surface view for a camera so it renders on the surface whenever is in the view of the camera. At the moment all I can see on my camera view is a black screen view. I have tried to look on Google and here but so far I haven’t found what I am looking for. Anyone can suggest me some idea.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:29:36+00:00Added an answer on June 3, 2026 at 12:29 pm

    I have written a class that can help you.

        public class Preview_can_work extends Activity {
            private SurfaceView surface_view;  
            private Camera mCamera;
            SurfaceHolder.Callback sh_ob = null;
            SurfaceHolder surface_holder        = null;
            SurfaceHolder.Callback sh_callback  = null;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
    
                getWindow().setFormat(PixelFormat.TRANSLUCENT);
    
                surface_view = new SurfaceView(getApplicationContext());
                addContentView(surface_view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    
                if (surface_holder == null) {
                    surface_holder = surface_view.getHolder();
                }
    
                sh_callback = my_callback();
                surface_holder.addCallback(sh_callback);
            }
    
                SurfaceHolder.Callback my_callback() {      
                    SurfaceHolder.Callback ob1 = new SurfaceHolder.Callback() {
    
                        @Override
                        public void surfaceDestroyed(SurfaceHolder holder) {
                              mCamera.stopPreview();
                              mCamera.release();
                              mCamera = null;
                        }
    
                        @Override
                        public void surfaceCreated(SurfaceHolder holder) {
                            mCamera = Camera.open();
    
                              try {
                                   mCamera.setPreviewDisplay(holder);  
                              } catch (IOException exception) {  
                                    mCamera.release();  
                                    mCamera = null;  
                              }
                        }
    
                        @Override
                        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                int height) {
                            mCamera.startPreview();
                        }
                    };
                    return ob1;
            }
        }
    

    in your manifest file copy this code for camera permission

    <uses-permission android:name="android.permission.CAMERA"/>
    

    Explanation:

    SurfaceView is a type of View which contains a SurfaceHolder. SurfaceHolder holds the surface on which we can display our media (generally frames).

    mCamera is a Camera object which will contains the camera instance.

    When you want to hold default Camera instance then you can simply call Camera.open();

    Camera mCamera = Camera.open();
    

    Now you have an open camera or you are having default camera instance. Now you need to capture frames from the camera and display it on a surface. But you cannot display it without any

    surface. Here the surfaceView provides surfaceHolder and surfaceHolder provides surface to display camera frames. Now when surface will be created three callback functions will be

    called.

    1. public void surfaceCreated(SurfaceHolder holder)
    2. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
    3. public void surfaceDestroyed(SurfaceHolder holder)
    

    Note:- Surface will be destroyed when your application will go on pause.

    surfaceCreated:
    surfaceCreated is a callback function which will be called when your surface will be created. In this, you can open your camera and set other attributes.

    surfaceChanged:
    This will be called atleast one time when your surface will be created. After that it will be called whenever your surface will change(In device rotation). Here you can

    start your preview because your surface have already created.

    surfaceDestroyed:
    This will be called every time when your surface will destroy. Now if you dont have surface then where you can display you camera frames so I have released camera by using

    mCamera.release(). This is very important because if your activity will be on pause and any other activity tries to open camera then it will not able to open it as you have

    already open camera. Camera is a shared resource so one time only one application can use it. So remember one thing whenever you open a camera then always release it.

    stopPreview:
    When you start preview then your camera starts capturing your frames and display it on a surface. Now if your surface have destroyed then you need to stop capturing frames

    from camera so you have to call mCamera.stopPreview.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create moving text.I have crated surface view and thread for looping
I am trying to implement AJax on Umbraco 5(jupiter), i have tried to create
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create a macro which can be used for print debug messages when
Trying to create a generic video plugin for Expression Engine 2 where I can
I am trying to create a simple 3-D app for android that will have
I have created an application where i have a camera preview. I am trying
hi i am trying to create a URL that looks like this: black/granite/worktops where
I am trying to create a parent WPF/surface application which will host multiple WPF/surface
I'm trying to create geometry to represent the Earth in OpenGL. I have what's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.