我写了这个相机应用程序,但我无法在每台设备上获得正确的图像
我发现不同设备上有两种类型的相机,所以我无法在每台设备上正确旋转照片。
我已经查看了一些开源相机应用,但没有看到任何处理此问题的逻辑,这些应用仍然可以正确旋转。
如何检测相机的类型?这样我就可以将照片旋转到正确的程度。
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;
}
}
}
答案 0 :(得分:0)
如果filepath of image
scaled bitmap
image
public Bitmap checkRotation(String filePath, Bitmap scaledBitmap) {
ExifInterface exif;
try {
exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 0);
Log.d("EXIF", "Exif: " + orientation);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 3) {
matrix.postRotate(180);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 8) {
matrix.postRotate(270);
Log.d("EXIF", "Exif: " + orientation);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,
scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix,
true);
} catch (IOException e) {
e.printStackTrace();
}
return scaledBitmap;
}
,则可以使用以下方法进行正确的轮换:
{{1}}