5

Hello friends. I am creating an Android application which uses camera code. I have used android-API 2.1 . I have installed my application on dell mobile and the orientation of camera is correct. However, I installed my application in other android mobiles like Samsung Galaxy S2 etc. Here, the problem is with camera orientation, even in portrait mode the camera is rotated 90 degrees. Can anyone suggest me if any camera parameters need to be changed so that camera orientation works well on all mobiles...? Here is my code:

//class CameraDemo
public class CameraDemo extends Activity {
private static final String TAG = "CameraDemo";
Camera camera;
Preview preview;
Button buttonClick;
Button next;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
try
{

super.onCreate(savedInstanceState);
setContentView(R.layout.camdemo);

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

buttonClick = (Button) findViewById(R.id.buttonClick);
next = (Button) findViewById(R.id.next);
buttonClick.setOnClickListener( new OnClickListener() {
public void onClick(View v) {

preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);

try{

Constants.takepic=1;

buttonClick.setEnabled(false); //once click is clicked, its disable
}
catch(Exception e)
{
e.printStackTrace();
}
}


});
next.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
//preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
if(Constants.takepic==1)
{
Intent intent_new=new Intent(CameraDemo.this,NotesAndUpload.class);
startActivity(intent_new);
}
else
{
Toast.makeText(CameraDemo.this, "Take a picture",5);
}

}


});

Log.d(TAG, "onCreate'd");
}
catch(Exception e ){}
}


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");
}
};

/** Handles data for jpeg picture */
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
//taking time and date
Constants.dateValue=dateAndTime("yyyy-MM-dd");
Constants.timeValue=dateAndTime("HH:mm:ss");
FileOutputStream outStream = null;
try {
// write to local sandbox file system
// outStream =
CameraDemo.this.openFileOutput(String.format("%d.j pg",
System.currentTimeMillis()), 0);
// Or write to sdcard
long imageNameLong=System.currentTimeMillis();
Constants.imageName=new Long(imageNameLong).toString();
outStream = new
FileOutputStream(String.format("/sdcard/%d.jpg",imageNameLong));
outStream.write(data);
Constants.takepic=1;
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");
}
};

}


//Preview Class
class Preview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "Preview";

SurfaceHolder mHolder;
public Camera camera;

Preview(Context context) {
super(context);

// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BU FFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
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) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
camera.stopPreview();
camera = null;
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
//CAMERA PARAMETERS NEED TO BE CHANGED :-
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(w, h);
if(getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT)
{
parameters.set("rotation",90);
parameters.set("orientation", "portrait");
}
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE)
{
parameters.set("rotation", 90);
parameters.set("orientation", "landscape");

}
parameters.set("jpeg-quality", 50);
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 );
}
}
nbanic
  • 1,248
  • 1
  • 8
  • 11
Shinchan
  • 51
  • 1
  • 2
  • Have a look at my answer to a similar camera screen rotation problem [here](http://stackoverflow.com/questions/3841122/android-camera-preview-is-sideways/5110406#5110406) It may help your current problem. – John J Smith Jul 13 '11 at 12:10

1 Answers1

0

If it gives you any error please post it, or use Log.d to show what Parameters is returning your Dell and using SendLog to see what the Samsung Galaxy is returning too. I think it's very important to see it, because I'd some problems with other parameters and I solved them like that.

nbanic
  • 1,248
  • 1
  • 8
  • 11
axierjhtjz
  • 6,647
  • 6
  • 47
  • 65