Android摄像头的设置定位入门意图ACTION_IMAGE_CAPTURE [英] set orientation of android camera started with intent ACTION_IMAGE_CAPTURE
问题描述
我在机器人,它使用相机拍摄photos.For开始我使用的是意图
ACTION_IMAGE_CAPTURE <摄像头的应用程序工作/ code>是这样的:
I'm working at an application in android which uses camera to take photos.For starting the camera I'm using an intent
ACTION_IMAGE_CAPTURE
like this:
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image=new File(Environment.getExternalStorageDirectory(),"PhotoContest.jpg");
camera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(image));
imageUri=Uri.fromFile(image);
startActivityForResult(camera,1);
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case 1:
if (resultCode == Activity.RESULT_OK) {
selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
image= (ImageView) findViewById(R.id.imageview);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
image.setImageBitmap(bitmap);
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
else
if(resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(EditPhoto.this, "Picture could not be taken.", Toast.LENGTH_SHORT).show();
}
}
}
的问题是,所有的拍摄的旋转与90中的照片度-水平对准
The problem is that all the photos that are taken are rotated with 90 degrees-horizontally aligned.
我也要把它放到我的清单文件:
I've also put this into my manifest file:
<activity android:name=".EditPhoto">
android:screenOrientation="portrait"
</activity>
但还是没有结果!因此,谁能帮助我???
But still with no result!So can anyone help me???
推荐答案
<一个href="http://developer.android.com/reference/android/media/ExifInterface.html">http://developer.android.com/reference/android/media/ExifInterface.html
<一个href="http://developer.android.com/reference/android/media/ExifInterface.html#TAG_ORIENTATION">http://developer.android.com/reference/android/media/ExifInterface.html#TAG_ORIENTATION
因此,如果在
Activity.onActivityResult(data, request, result) {
if (request == PHOTO_REQUEST && result == RESULT_OK) {
...
Uri imageUri = ...
File imageFile = new File(imageUri.toString());
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate-=90;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate-=90;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate-=90;
}
Canvas canvas = new Canvas(bitmap);
canvas.rotate(rotate);
}
这是否帮助呢?
Does this help at all?
我想补充格雷格最伟大的答案,这里是一个整体的类别来完成这项工作:
Just to add to Greg's great answer, here's a whole "category" to do the job:
public static int neededRotation(File ff)
{
try
{
ExifInterface exif = new ExifInterface(ff.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
if (orientation == ExifInterface.ORIENTATION_ROTATE_270)
{ return 270; }
if (orientation == ExifInterface.ORIENTATION_ROTATE_180)
{ return 180; }
if (orientation == ExifInterface.ORIENTATION_ROTATE_90)
{ return 90; }
return 0;
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return 0;
}
你会用它或多或少像这样...
you'd use it more or less like this ...
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_IMAGE_CAPTURE) // && resultCode == RESULT_OK )
{
try
{
Bitmap cameraBmp = MediaStore.Images.Media.getBitmap(
State.mainActivity.getContentResolver(),
Uri.fromFile( Utils.tempFileForAnImage() ) );
cameraBmp = ThumbnailUtils.extractThumbnail(cameraBmp, 320,320);
// NOTE incredibly useful trick for cropping/resizing square
// http://stackoverflow.com/a/17733530/294884
Matrix m = new Matrix();
m.postRotate( Utils.neededRotation(Utils.tempFileForAnImage()) );
cameraBmp = Bitmap.createBitmap(cameraBmp,
0, 0, cameraBmp.getWidth(), cameraBmp.getHeight(),
m, true);
yourImageView.setImageBitmap(cameraBmp);
// to convert to bytes...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
cameraBmp.compress(Bitmap.CompressFormat.JPEG, 75, baos);
//or say cameraBmp.compress(Bitmap.CompressFormat.PNG, 0, baos);
imageBytesRESULT = baos.toByteArray();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return;
}
}
希望这样可以节省有人在未来的某个打字。
Hope it saves someone some typing in the future.
这篇关于Android摄像头的设置定位入门意图ACTION_IMAGE_CAPTURE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!