Hi all, I am trying to build a simple camera view as part of my application. I want it to work just like the built in camera What you see is what you get. It doesn't auto correct rotation and the resultant photo is in the right orientation.
What is happening in my Cameraview is that when i hold the phone in portrait (like a normal phone) what i see is rotated 90 degrees anti clockwise. The only time the camera shows me what i want to see is when I hold it rotated 90 degrees anti clockwise. As i rotate the phone it flickers and changes my display rotation. I just want it to be fixed so that i always see a preview the right way up, no matter which rotation I hold the phone. I have searched lots of forums, I have even tried looking at the android camera code but I have added some orientation code and it doesn't seem to do anything. The only way i got it to work was to use this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) in the SurfaceCreated method. But the resultant photos are rotated incorrectly. Can anyone give me a guiding hand in this please? I will put my code beneath so you can see what i am doing wrong! package com.pengilleys.fishingsnapz; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.view.OrientationEventListener; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.Toast; public class CameraView extends Activity implements SurfaceHolder.Callback{ private Camera mCamera; private boolean mPreviewRunning=true; private Context mContext = this; private static String fullImagePath = ""; private OrientationEventListener mOrientationListener; private int mLastOrientation = 0; // No rotation (landscape) by default. private Parameters mParameters; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); getWindow().setFormat(PixelFormat.TRANSLUCENT); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.camera_layout); SurfaceView mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera); SurfaceHolder mSurfaceHolder = mSurfaceView.getHolder(); mSurfaceHolder.addCallback(this); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mOrientationListener = new OrientationEventListener(CameraView.this) { public void onOrientationChanged(int orientation) { // We keep the last known orientation. So if the user // first orient the camera then point the camera to if (orientation != 0) { orientation += 90; } if (orientation != mLastOrientation) { mLastOrientation = orientation; } } }; //Button snap = (Button)findViewById(R.id.snap); //snap.setOnClickListener(new View.OnClickListener(){ mSurfaceView.setOnClickListener(new OnClickListener(){ public void onClick(View v){ try{ mCamera.autoFocus(new Camera.AutoFocusCallback() { public void onAutoFocus(boolean success, Camera camera) { mCamera.takePicture(null, mPictureCallback, mPictureCallback); } }); }catch(Exception e){ Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); } }}); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { if (mPreviewRunning) { mCamera.stopPreview(); } Camera.Parameters p = mCamera.getParameters(); p.setPreviewSize(w, h); mCamera.setParameters(p); try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } mCamera.startPreview(); mPreviewRunning = true; } @Override public void surfaceCreated(SurfaceHolder holder) { mOrientationListener.enable(); //this.setRequestedOrientation //(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); mCamera = Camera.open(); try{ mCamera.setPreviewDisplay(holder); }catch(IOException e){ mCamera.release(); mCamera = null; } } @Override public void surfaceDestroyed(SurfaceHolder holder) { //mCamera.stopPreview(); mPreviewRunning = false; mCamera.release(); } Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { public void onPictureTaken(byte[] imageData, Camera c) { mParameters.setRotation(mLastOrientation); try{ if(imageData!=null){ StoreByteImage(getApplicationContext(),imageData,100,"Imagename"); try{ Intent mIntent = new Intent(mContext,SnapDetails.class); mIntent.putExtra("imgPath",fullImagePath); startActivityForResult(mIntent,1); //finish(); }catch(Exception e){ Toast.makeText(mContext, e.toString(), Toast.LENGTH_LONG); } } }catch(Exception e){ e.printStackTrace(); } } }; public static boolean StoreByteImage(Context mContext, byte[] imageData, int quality, String expName) { try{ File sdImageMainDir = new File("/sdcard/fishsnapz/"); sdImageMainDir.mkdir(); Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, null) ; String fileName = Long.toString(System.currentTimeMillis()) + "jpg"; fullImagePath = sdImageMainDir + fileName; FileOutputStream output = new FileOutputStream(fullImagePath); BufferedOutputStream buf = new BufferedOutputStream(output); bitmap.compress(Bitmap.CompressFormat.JPEG, quality, buf); buf.flush(); buf.close(); }catch(FileNotFoundException e){ Toast.makeText(mContext, e.toString(), Toast.LENGTH_LONG).show(); }catch(IOException e){ Toast.makeText(mContext, e.toString(), Toast.LENGTH_LONG).show(); } return true; } protected void onResume() { super.onResume(); // mCamera.startPreview(); } protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } protected void onStop() { super.onStop(); } protected void onPause(){ //mCamera.release(); super.onPause(); } } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en