如何设置相机图像方向?
how to set camera Image orientation?
在我的应用程序中,我添加了图片上传功能,它适用于除相机图像之外的所有图像,每当我浏览图库中的相机图像和旋转 90 度的肖像图像时..以下是我的代码片段..有人能帮我吗?我学习了很多教程,但它们在 kikat 中都能很好地工作..但是当相同的教程不适用于 ics、jellybean 等时..
public class MainActivity extends Activity {
private Button browse;
private String selectedImagePath="";
private ImageView img;
private TextView messageText;
private static int SELECT_PICTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.imagevw);
browse=(Button)findViewById(R.id.browseimg);
messageText = (TextView)findViewById(R.id.messageText);
browse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
/*String filePath = getRealPathFromURI(getActivity(), selectedImageUri );
messageText.setText(filePath );
Picasso.with(getActivity())
.load(new File(filePath ))
.centerCrop()
.resize(60, 60).into( img);*/
selectedImagePath = getPath(selectedImageUri);
messageText.setText(selectedImagePath);
System.out.println(requestCode);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
@SuppressWarnings("deprecation")
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
有几种方法,但我发现最简单的方法是使用 Picasso 库。由于是上传案例,我们会得到正确的方向,也可以调整图像位图大小。
String filePath = getRealPathFromURI(getActivity(), selectedImageUri );
messageText.setText(filePath );
Picasso.with(getActivity())
.load(new File(filePath ))
.centerCrop()
.resize(60, 60).into( img);
只需包含此代码
public void rotateImage(String file) throws IOException{
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
int rotationAngle = getCameraPhotoOrientation(getActivity(), Uri.fromFile(file1), file1.toString());
Matrix matrix = new Matrix();
matrix.postRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
FileOutputStream fos=new FileOutputStream(file);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
int rotate = 0;
try {
context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
rotate = 0;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}
我用下面的代码解决了图片旋转问题
public Bitmap rotateImageIfRequired(String imagePath) {
int degrees = 0;
try {
ExifInterface exif = new ExifInterface(imagePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degrees = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degrees = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degrees = 270;
break;
}
} catch (IOException e) {
Log.e("ImageError", "Error in reading Exif data of " + imagePath, e);
}
BitmapFactory.Options decodeBounds = new BitmapFactory.Options();
decodeBounds.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, decodeBounds);
int numPixels = decodeBounds.outWidth * decodeBounds.outHeight;
int maxPixels = 2048 * 1536; // requires 12 MB heap
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = (numPixels > maxPixels) ? 2 : 1;
bitmap = BitmapFactory.decodeFile(imagePath, options);
if (bitmap == null) {
return null;
}
Matrix matrix = new Matrix();
matrix.setRotate(degrees);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
return bitmap;
}
在我的应用程序中,我添加了图片上传功能,它适用于除相机图像之外的所有图像,每当我浏览图库中的相机图像和旋转 90 度的肖像图像时..以下是我的代码片段..有人能帮我吗?我学习了很多教程,但它们在 kikat 中都能很好地工作..但是当相同的教程不适用于 ics、jellybean 等时..
public class MainActivity extends Activity {
private Button browse;
private String selectedImagePath="";
private ImageView img;
private TextView messageText;
private static int SELECT_PICTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.imagevw);
browse=(Button)findViewById(R.id.browseimg);
messageText = (TextView)findViewById(R.id.messageText);
browse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
/*String filePath = getRealPathFromURI(getActivity(), selectedImageUri );
messageText.setText(filePath );
Picasso.with(getActivity())
.load(new File(filePath ))
.centerCrop()
.resize(60, 60).into( img);*/
selectedImagePath = getPath(selectedImageUri);
messageText.setText(selectedImagePath);
System.out.println(requestCode);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
@SuppressWarnings("deprecation")
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
有几种方法,但我发现最简单的方法是使用 Picasso 库。由于是上传案例,我们会得到正确的方向,也可以调整图像位图大小。
String filePath = getRealPathFromURI(getActivity(), selectedImageUri );
messageText.setText(filePath );
Picasso.with(getActivity())
.load(new File(filePath ))
.centerCrop()
.resize(60, 60).into( img);
只需包含此代码
public void rotateImage(String file) throws IOException{
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
int rotationAngle = getCameraPhotoOrientation(getActivity(), Uri.fromFile(file1), file1.toString());
Matrix matrix = new Matrix();
matrix.postRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
FileOutputStream fos=new FileOutputStream(file);
rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
int rotate = 0;
try {
context.getContentResolver().notifyChange(imageUri, null);
File imageFile = new File(imagePath);
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
rotate = 0;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return rotate;
}
我用下面的代码解决了图片旋转问题
public Bitmap rotateImageIfRequired(String imagePath) {
int degrees = 0;
try {
ExifInterface exif = new ExifInterface(imagePath);
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degrees = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degrees = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degrees = 270;
break;
}
} catch (IOException e) {
Log.e("ImageError", "Error in reading Exif data of " + imagePath, e);
}
BitmapFactory.Options decodeBounds = new BitmapFactory.Options();
decodeBounds.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, decodeBounds);
int numPixels = decodeBounds.outWidth * decodeBounds.outHeight;
int maxPixels = 2048 * 1536; // requires 12 MB heap
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = (numPixels > maxPixels) ? 2 : 1;
bitmap = BitmapFactory.decodeFile(imagePath, options);
if (bitmap == null) {
return null;
}
Matrix matrix = new Matrix();
matrix.setRotate(degrees);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
return bitmap;
}