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 9134519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:40:33+00:00 2026-06-17T08:40:33+00:00

I have an application to get camera preview frames with a surface. It was

  • 0

I have an application to get camera preview frames with a surface. It was working on Android 4.0.4 but it does not work with Jelly Bean, 4.1.2 on the same device after the update. Simply, the callback is never called back. Here is the code: Snipped a little bit:

public class Panel extends Activity {
    Camera myCamera;
    int cameraId = -1;
    MyCameraSurfaceView myCameraSurfaceView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_panel);

        myCamera = getCameraInstance();
        myCamera.setPreviewCallback(new Camera.PreviewCallback() {
            @Override
            public void onPreviewFrame(byte[] data, Camera camera) {
                Log.d("Camera Preview", data.length + "");
            }
        });

        myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
        FrameLayout myCameraPreview = (FrameLayout) findViewById(R.id.videoview);
        myCameraPreview.addView(myCameraSurfaceView);
    }

    private Camera getCameraInstance() {
        Camera c = null;
        try {
            c = Camera.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }


    public class MyCameraSurfaceView extends SurfaceView implements
            SurfaceHolder.Callback {

        private SurfaceHolder mHolder;
        private Camera mCamera;

        public MyCameraSurfaceView(Context context, Camera camera) {
            super(context);
            mCamera = camera;
            mHolder = getHolder();
            mHolder.addCallback(this);
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            } catch (IOException e) {
            }
        }
    }
}

The video frames are being displayed on the activity, so I cannot figure out what I am doing wrong.

  • 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-17T08:40:34+00:00Added an answer on June 17, 2026 at 8:40 am

    I have rewrote the code by using another tutorial. It works, but now slower. I don’t know the exact reason (it may be due to leaked N7000 ROM I use, it may have a bug or this is implemented differently in 4.1, not sure)

    CameraPreview.java

    public class CameraPreview implements SurfaceHolder.Callback,
            Camera.PreviewCallback {
        int PreviewSizeWidth;
        int PreviewSizeHeight;
        SurfaceHolder mSurfHolder;
        Camera mCamera;
    
        public CameraPreview(int PreviewlayoutWidth, int PreviewlayoutHeight) {
            PreviewSizeWidth = PreviewlayoutWidth;
            PreviewSizeHeight = PreviewlayoutHeight;
        }
    
    
    
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            Parameters p = camera.getParameters();  
            int width = p.getPreviewSize().width;
            int height = p.getPreviewSize().height;
    
            ByteArrayOutputStream outstr = new ByteArrayOutputStream();
            Rect rect = new Rect(0, 0, width, height);
            YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, width,
                    height, null);
            yuvimage.compressToJpeg(rect, 80, outstr); // outstr contains image in jpeg  
            String encodedImage = Base64.encodeToString(
                    outstr.toByteArray(), Base64.DEFAULT); // this is base64 encoding of image
    
    
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
            Parameters parameters;
            mSurfHolder = arg0;
    
            parameters = mCamera.getParameters();
            parameters.setPreviewSize(PreviewSizeWidth, PreviewSizeHeight);
    
            mCamera.setParameters(parameters);
            mCamera.startPreview();
        }
    
        public void surfaceCreated(SurfaceHolder arg0) {
            mCamera = Camera.open();
            try {
                // If did not set the SurfaceHolder, the preview area will be black.
                mCamera.setPreviewDisplay(arg0);
                mCamera.setPreviewCallback(this);
                Parameters p = mCamera.getParameters();
                p.setPreviewSize(PreviewSizeWidth, PreviewSizeHeight);
                mCamera.setParameters(p);
            } catch (IOException e) {
                mCamera.release();
                mCamera = null;
            }
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder arg0) {
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }
    

    PanelActivity.java

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_panel);
    
    
        SurfaceView camView = new SurfaceView(this);
        SurfaceHolder camHolder = camView.getHolder();
        int width = 352; // must set a compatible value, otherwise it gets the default width and height
        int height = 288;
    
        camPreview = new CameraPreview(width, height);
    
        camHolder.addCallback(camPreview);
        camHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
        mainLayout = (FrameLayout) findViewById(R.id.videoview);
        mainLayout.addView(camView, new LayoutParams(width, height));
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an application where i have a camera preview. I am trying
I have an application that uses camera to take images. Application works but is
I have recently built an application that shows you a camera preview on screen.
I have application that opens native camera app and get the captured picture. I
In my application, I have a SurfaceView showing a Camera preview, and want to
I have a variable in my Sinatra application: get '/' do @agenda_date = Date.today
I have a C# application..I continuously get a null reference exception..I manage to catch
I have an application (ASP.NET MVC) that uses a Next New method to get
In my application I have to get the record form the AddressBook and after
I have a php application that uses the google feed api to get the

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.