I wrote this camera app, but I cannot got correct image on every device
I found that there are two types camera on different devices, so I cannot rotate photo correctly on every device.
I have looked into some open source camera app, but don't see any logic to handle this problem, and these apps still rotate correctly.
How to detect the type of camera? so that I can rotate photo to correct degree.
public class Camera01 extends Activity implements SurfaceHolder.Callback {
SurfaceHolder surfaceHolder;
SurfaceView surfaceView1;
Button button1;
Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera01);
button1=(Button)findViewById(R.id.button1);
surfaceView1=(SurfaceView)findViewById(R.id.surfaceView1);
surfaceHolder=surfaceView1.getHolder();
surfaceHolder.addCallback(this);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
camera.autoFocus(afcb);
}
});
}
PictureCallback jpeg =new PictureCallback(){
public void onPictureTaken(byte[] data, Camera camera) {
new SaveImageTask().execute(data);
camera.startPreview();
}
};
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
public void surfaceCreated(SurfaceHolder holder) {
camera=Camera.open(1);
try {
Camera.Parameters params = camera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();
params.setPictureFormat(PixelFormat.JPEG);
Camera.Size mSize = sizes.get(0);
params.setPictureSize(mSize.width, mSize.height);
params.setRotation(90); // <<<<<<<<<<<<<<<<<<<<< to correct photo direction
camera.setParameters(params);
camera.setPreviewDisplay(surfaceHolder);
// camera.setDisplayOrientation(90);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
}
AutoFocusCallback afcb= new AutoFocusCallback(){
public void onAutoFocus(boolean success, Camera camera) {
if(success){
camera.takePicture(null, null, jpeg);
}
}
};
private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
@Override
protected Void doInBackground(byte[]... data) {
FileOutputStream outStream = null;
try {
File dir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), "test");
if (! dir.exists()){
if (! dir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
outStream.write(data[0]);
outStream.flush();
outStream.close();
Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(outFile));
sendBroadcast(mediaScanIntent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return null;
}
}
}