Java 正在旋转的垂直图像
我正在使用以下代码在我的应用程序中拍摄并保存照片:Java 正在旋转的垂直图像,java,android,Java,Android,我正在使用以下代码在我的应用程序中拍摄并保存照片: public void addPhoto(){ Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // Create the
public void addPhoto(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID,
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==REQUEST_IMAGE_CAPTURE&&resultCode==RESULT_OK){
Intent i = new Intent(Assignments.this, AddAssignment.class);
i.putExtra("fileName", mCurrentPhotoPath);
startActivity(i);
}
}
public Bitmap getImage(){
int targetW;
int targetH;
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileName, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
if(photoW>photoH){
targetW=400;
targetH=300;
}else{
targetW=300;
targetH=400;
}
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(fileName, bmOptions);
photo=bitmap;
return(bitmap);
}
然后,我使用以下代码缩放图像以创建较小尺寸的预览:
public void addPhoto(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID,
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==REQUEST_IMAGE_CAPTURE&&resultCode==RESULT_OK){
Intent i = new Intent(Assignments.this, AddAssignment.class);
i.putExtra("fileName", mCurrentPhotoPath);
startActivity(i);
}
}
public Bitmap getImage(){
int targetW;
int targetH;
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileName, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
if(photoW>photoH){
targetW=400;
targetH=300;
}else{
targetW=300;
targetH=400;
}
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(fileName, bmOptions);
photo=bitmap;
return(bitmap);
}
我的问题是,如果拍摄的图像是垂直的,则图像会水平翻转。我不知道这到底发生在哪里,我希望能得到任何帮助。谢谢。一些设备摄像头使用
Exif
标志。这是他们在自然旋转中存储它的地方,用一个旗子来代表它
下面是我为在内存中旋转位图而编写的一些代码。请酌情使用/申请
关于exif标志的信息:
助手方法:
private static float exifToDegrees(float exifOrientation) {
// MAY NEED TO HANDLE THE OTHER FLAGS TOO, though I don't think android flips photos.
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
}
return 0;
}
一些设备摄像头使用
Exif
标志。这是他们在自然旋转中存储它的地方,用一个旗子来代表它
下面是我为在内存中旋转位图而编写的一些代码。请酌情使用/申请
关于exif标志的信息:
助手方法:
private static float exifToDegrees(float exifOrientation) {
// MAY NEED TO HANDLE THE OTHER FLAGS TOO, though I don't think android flips photos.
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
}
return 0;
}
如果您的设备摄像头以这种方式工作,可能与“Exif”有关。而你并没有对此负责。如果您的设备摄像头以这种方式工作,可能与“Exif”有关。而你并没有对此负责。非常感谢。我是个大傻瓜,但谢谢你教我一些东西。@Mateuszplaza没问题。当我第一次遇到它时,它也让我感到困惑非常感谢。我是个大傻瓜,但谢谢你教我一些东西。@Mateuszplaza没问题。当我第一次遇到它时,它也让我感到困惑