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

  • Home
  • SEARCH
  • 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 7893595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:10:21+00:00 2026-06-03T07:10:21+00:00

This camera program showing horizontal preview while we are watching vertically like its was

  • 0

This camera program showing horizontal preview while we are watching vertically like its was showing 90’angle and also it was not storing into my SD card ..any body help me how to change my surface angle and how it will be changed into SD card ??

1.my preview class:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
class Preview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "Preview";
    SurfaceHolder mHolder;
    public Camera camera;
     Preview(Context context) {
        super(context);
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
        try {
            camera.setPreviewDisplay(holder);


            camera.setPreviewCallback(new PreviewCallback() {

                public void onPreviewFrame(byte[] data, Camera arg1) {
                    FileOutputStream outStream = null;
                    try {
                        outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));  
                        outStream.write(data);
                        outStream.close();
                        Log.d(TAG, "onPreviewFrame - wrote bytes: " + data.length);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                    }
                        Preview.this.invalidate();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera = null;
    }
        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(w, h);
        camera.setParameters(parameters);
        camera.startPreview();
    }

    @Override
    public void draw(Canvas canvas) {
            super.draw(canvas);
            Paint p= new Paint(Color.RED);
            Log.d(TAG,"draw");
            canvas.drawText("PREVIEW", canvas.getWidth()/2, canvas.getHeight()/2, p );
    }
}

2.MYCameraDemo class

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
public class CameraDemo extends Activity {
    private static final String TAG = "CameraDemo";
    Camera camera;
    Preview preview;
    Button buttonClick;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        preview = new Preview(this);
        ((FrameLayout) findViewById(R.id.preview)).addView(preview);

        buttonClick = (Button) findViewById(R.id.buttonClick);
        buttonClick.setOnClickListener( new OnClickListener() {
            public void onClick(View v) {
                preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
            }
        });

        Log.d(TAG, "onCreate'd");
    }
    ShutterCallback shutterCallback = new ShutterCallback() {
        public void onShutter() {
            Log.d(TAG, "onShutter'd");
        }
    };

    /** Handles data for raw picture */
    PictureCallback rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.d(TAG, "onPictureTaken - raw");
        }
    };

    PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            FileOutputStream outStream = null;
            try {
                        outStream = CameraDemo.this.openFileOutput(String.format("%d.jpg", System.currentTimeMillis()), 0); 

                outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis()));  
                outStream.write(data);
                outStream.close();
                Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            }
            Log.d(TAG, "onPictureTaken - jpeg");
        }
    };
}
  • 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-03T07:10:22+00:00Added an answer on June 3, 2026 at 7:10 am

    Perhaps this link can help you.

    http://adblogcat.com/a-camera-preview-with-a-bounding-box-like-google-goggles/

    Here is the onSurfaceChange method which sets the right orientation

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        //Log.i(TAG, "Preview: surfaceChanged() - size now " + w + "x" + h);
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        try {
            mParameters = mCamera.getParameters();
            mParameters.set("orientation","landscape");
            for (Integer i : mParameters.getSupportedPreviewFormats()) {
                //Log.i(TAG, "supported preview format: " + i);
            }
    
            List<Size> sizes = mParameters.getSupportedPreviewSizes();
            for (Size size : sizes) {
                //Log.i(TAG, "supported preview size: " + size.width + "x" + size.height);
            }
            mCamera.setParameters(mParameters); // apply the changes
        } catch (Exception e) {
            // older phone - doesn't support these calls
        }
    
        //updateBufferSize(); // then use them to calculate
    
        Size p = mCamera.getParameters().getPreviewSize();
        //Log.i(TAG, "Preview: checking it was set: " + p.width + "x" + p.height); // DEBUG
        mCamera.startPreview();
    }
    
    public Parameters getCameraParameters(){
        return mCamera.getParameters();
    }
    

    The article sets the parameters to landscape. I see you do not have this in your code.

    Also, for saving images I see two methods:

    private boolean savePhoto(Bitmap bm) {
    FileOutputStream image = null;
    try {
        image = new FileOutputStream(mLocation);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bm.compress(CompressFormat.JPEG, 100, image);
    if (bm != null) {
        int h = bm.getHeight();
        int w = bm.getWidth();
        //Log.i(TAG, "savePhoto(): Bitmap WxH is " + w + "x" + h);
    } else {
        //Log.i(TAG, "savePhoto(): Bitmap is null..");
        return false;
    }
    return true;
    

    }

    and

    public Bitmap getPic(int x, int y, int width, int height) {
    System.gc();
    Bitmap b = null;
    Size s = mParameters.getPreviewSize();
    
    YuvImage yuvimage = new YuvImage(mBuffer, ImageFormat.NV21, s.width, s.height, null);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    yuvimage.compressToJpeg(new Rect(x, y, width, height), 100, outStream); // make JPG
    b = BitmapFactory.decodeByteArray(outStream.toByteArray(), 0, outStream.size());
    if (b != null) {
        //Log.i(TAG, "getPic() WxH:" + b.getWidth() + "x" + b.getHeight());
    } else {
        //Log.i(TAG, "getPic(): Bitmap is null..");
    }
    yuvimage = null;
    outStream = null;
    System.gc();
    return b;
    

    }

    And you need to use android 2.2

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

Sidebar

Related Questions

i'm trying the camera preview This is my code and it doesn't throw any
I've run into this issue when trying to fire up a camera intent like
I am creating a program to talk to a IP Camera, of this model:
I wish to program with the Kinect ToF camera, however I am not certain
I have a question this time around regarding the Android custom camera, NOT the
I'd like to port a Linux C program to Java. This program controls a
I'm trying to draw something on the camera preview. But when the program call
This is really just for my own use: I would like to be able
This somehow simple task is not so simple. I can get the number of
I'm just playing around with a simple program that opens the camera. That's literally

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.