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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:11:31+00:00 2026-05-19T11:11:31+00:00

I am developing an Android application that relies on the camera API, using an

  • 0

I am developing an Android application that relies on the camera API, using an HTC EVO as my testing device. No matter what I’ve tried so far, the only time the camera preview looks right is in landscape mode (90 degrees rotation, to be specific). It seems as though there is no way to orient the preview correctly when in portrait mode (0 degrees rotation).

The default camera application on the device (for HTC Sense) allows for any kind of rotation without any problem, so I know that there is no hardware limitation. I even downloaded some source code from HTC’s developer site, but it’s all in C – kernel stuff, apparently.

Can anyone point me in the right direction for solving this problem? Is there a way to rotate the preview correctly in Android 2.1 or 2.2?

Thank you.

P.S. Here is the code I’m using, in case it helps…

package spikes.cameraSpike03;

import java.util.List;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity implements SurfaceHolder.Callback {
 private static final String LOG_TAG = "spikes.cameraSpike03 - MainActivity";

 private Camera _camera;
 private boolean _previewIsRunning = false;

 private SurfaceView _svCameraView;
 private SurfaceHolder _surfaceHolder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);

        _svCameraView = (SurfaceView)findViewById(R.id.svCameraView);

        _surfaceHolder = _svCameraView.getHolder();
        _surfaceHolder.addCallback(this);
        _surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  if(_previewIsRunning){
   _camera.stopPreview();
  }

  try{
   Camera.Parameters parameters = _camera.getParameters();

   //Get the optimal preview size so we don't get an exception when setting the parameters 
   List<Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
   Size optimalPreviewSize = getOptimalPreviewSize(supportedPreviewSizes, width, height);
   parameters.setPreviewSize(optimalPreviewSize.width, optimalPreviewSize.height);

   _camera.setParameters(parameters);

   _camera.setPreviewDisplay(holder);
  }
  catch(Exception ex){
   ex.printStackTrace();
   Log.e(LOG_TAG, ex.toString());
  }

  _camera.startPreview();
  _previewIsRunning = true;
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  _camera = Camera.open();
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  _camera.stopPreview();
  _previewIsRunning = false;
  _camera.release();
 }

 private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.05;
        double targetRatio = (double) w / h;
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

}
  • 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-05-19T11:11:32+00:00Added an answer on May 19, 2026 at 11:11 am

    Try locking the orientation of the Activity in horizontal if that fits with your design, or have a play with these:

    private android.hardware.Camera mCamera;
    

    …

    mCamera.setDisplayOrientation(90);
    params.setRotation(90);
    mCamera.setParameters(params);
    

    But I think setDisplayOrientation might be 2.2 only…

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

Sidebar

Related Questions

I'm currently developing an Android application that fetches images using http requests. It would
Hi I am developing an application for the android htc hero. I am looking
I'm developing an Android application that might be used at night. Therefor, I need
I'm developing an Android application that uses a WebView to display the login page
I am developing an android application that receives data about events from a server.
I'm developing an Android application that I'd like to be compatible with 1.5 (SDK
I'm developing an Android Application that has three very similar Activities. I would like
I'm developing an Android application using Android 1.5. I'm using an older version of
I'm developing an Android application that consists of: a lightweight background service that logs
I'm developing an Android application that uses a MapView . I'd like to run

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.