- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的应用程序拍摄肖像照片。
在画廊里我看到了
但是当保存到我的服务器时,我看到它是横向的。
我检查了我的图像文件并得到了
ORIENTATION_ROTATE_90
这是什么意思?
我尝试将其设置为ORIENTATION_NORAML
,但它仅以横向保存(无论是纵向还是横向拍摄)
ExifInterface exif;
try {
exif = new ExifInterface(imageFilename);
int orientation =
exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
exif.setAttribute(ExifInterface.TAG_ORIENTATION,
String.valueOf(ExifInterface.ORIENTATION_ROTATE_270));
//also tried ExifInterface.ORIENTATION_ROTATE_UNDEFINED)
exif.saveAttributes();
orientation =
exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
boolean b = orientation == Configuration.ORIENTATION_LANDSCAPE;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我该如何解决这个问题?
最佳答案
试试这个方法,希望能帮助您解决问题...
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:id="@+id/imgFromCameraOrGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnCamera"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Camera"/>
<Button
android:id="@+id/btnGallery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gallery"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private ImageView imgFromCameraOrGallery;
private Button btnCamera;
private Button btnGallery;
private String imgPath;
final private int PICK_IMAGE = 1;
final private int CAPTURE_IMAGE = 2;
private File mFileTemp;
public String TEMP_PHOTO_FILE_NAME = "temp_photo.jpg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgFromCameraOrGallery = (ImageView) findViewById(R.id.imgFromCameraOrGallery);
btnCamera = (Button) findViewById(R.id.btnCamera);
btnGallery = (Button) findViewById(R.id.btnGallery);
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mFileTemp = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE_NAME);
}
else {
mFileTemp = new File(getFilesDir(), TEMP_PHOTO_FILE_NAME);
}
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, CAPTURE_IMAGE);
}
});
btnGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CAPTURE_IMAGE) {
try {
InputStream inputStream = getContentResolver().openInputStream(getImageUri(getImagePath()));
FileOutputStream fileOutputStream = new FileOutputStream(mFileTemp);
copyStream(inputStream, fileOutputStream);
fileOutputStream.close();
inputStream.close();
}catch (Throwable e){
e.printStackTrace();
}
rotateImage(mFileTemp);
} else if (requestCode == PICK_IMAGE) {
imgFromCameraOrGallery.setImageBitmap(BitmapFactory.decodeFile(getAbsolutePath(data.getData())));
}
}
}
private void rotateImage(final File file) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Bitmap b = decodeFileFromPath(file.getPath());
try {
ExifInterface ei = new ExifInterface(file.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.postRotate(90);
b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.postRotate(180);
b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.postRotate(270);
b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
break;
default:
b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
break;
}
} catch (Throwable e) {
e.printStackTrace();
}
FileOutputStream out1 = null;
try {
if (mFileTemp.exists())
mFileTemp.delete();
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mFileTemp = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE_NAME);
} else {
mFileTemp = new File(getFilesDir(), TEMP_PHOTO_FILE_NAME);
}
out1 = new FileOutputStream(mFileTemp);
b.compress(Bitmap.CompressFormat.JPEG, 90, out1);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out1.close();
} catch (Throwable ignore) {
}
}
imgFromCameraOrGallery.setImageBitmap(BitmapFactory.decodeFile(mFileTemp.getAbsolutePath()));
}
});
}
private Bitmap decodeFileFromPath(String path){
Uri uri = getImageUri(path);
InputStream in = null;
try {
in = getContentResolver().openInputStream(uri);
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
int inSampleSize = 1024;
if (o.outHeight > inSampleSize || o.outWidth > inSampleSize) {
scale = (int) Math.pow(2, (int) Math.round(Math.log(inSampleSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
in = getContentResolver().openInputStream(uri);
Bitmap b = BitmapFactory.decodeStream(in, null, o2);
in.close();
return b;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String getAbsolutePath(Uri uri) {
if(Build.VERSION.SDK_INT >= 19){
String id = uri.getLastPathSegment().split(":")[1];
final String[] imageColumns = {MediaStore.Images.Media.DATA };
final String imageOrderBy = null;
Uri tempUri = getUri();
Cursor imageCursor = getContentResolver().query(tempUri, imageColumns,
MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);
if (imageCursor.moveToFirst()) {
return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
}else{
return null;
}
}else{
String[] projection = { MediaStore.MediaColumns.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
}
private Uri getUri() {
String state = Environment.getExternalStorageState();
if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED))
return MediaStore.Images.Media.INTERNAL_CONTENT_URI;
return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
}
private Uri getImageUri(String path) {
return Uri.fromFile(new File(path));
}
public void copyStream(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
}
public Uri setImageUri() {
// Store image in dcim
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");
Uri imgUri = Uri.fromFile(file);
this.imgPath = file.getAbsolutePath();
return imgUri;
}
public String getImagePath() {
return imgPath;
}
}
关于java - 为什么 `exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_ROTATE_270));` 什么也不做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23677098/
我的应用程序拍摄肖像照片。 在画廊里我看到了 但是当保存到我的服务器时,我看到它是横向的。 我检查了我的图像文件并得到了 ORIENTATION_ROTATE_90 这是什么意思? 我尝试将其设置为O
我用过ExifInterface在 Android 应用程序中用于获取图像的相机方向。我的代码完美适用于 Build.VERSION.SDK_INT = 24) {
我使用这段代码来获取有关 sdcard 中图像的信息, File file = (File) iterator.next(); ExifInterface ex
我正在尝试从画廊的图像中提取 Exif 数据,如下所示: @Override protected void onActivityResult(int requestCode, int resultCo
我想获取位图的方向。我使用这段代码: ExifInterface ei = new ExifInterface(photoPath); int orientation = ei.getAttribut
下面是我的代码- try { InputStream inputStream = getAssets().open("thumbnail.jpg"); exifInterface = new
当我将 picasso 从 2.5.2 升级到 2.71828 并执行 gradle sync 时,我看到了一个exif接口(interface)错误: 错误: Failed to resolve:
支持库版本 25.1.0 添加了一个新功能 ExifInterface,它向后移植了添加到 Android 7.1 中的 ExifInterface 的功能。 但是如何在不提供默认值的情况下检查特定文
我需要弄清楚如何从位图中获取方向。事情是 - 图像并不总是出现在设备上,所以我不能使用 ExifInterface 来获取方向信息。不知何故,它总是横向显示位图,长度和宽度属性对我帮助不大。 有谁知道
在尝试使用文件路径初始化 exif 接口(interface)实例时,我无法找出 ExifInterface 构造函数抛出的异常。 已更新请按要求查看下面的详细代码。 文件下载功能 public vo
Camera.Parameters 中的 setRotation 方法 并不适用于所有设备。有人建议手动更改EXIF信息来解决问题。你能给我一个简短的例子,说明如何使用 ExifInterface 设
我正在尝试将经纬度和其他数据写入自定义相机应用程序中 jpeg 的 Exif header 。通常,android 会自动使用光圈、ISO、快门速度等数据填充 header 。但是,当我手动添加创建一
我的问题是,在某些设备上,一些从图库中选取的图像在 ImageView 中旋转。我找到了很多使用ExifInterface来解决这个问题的资料,所以我尝试使用它。但是当我尝试使用图像路径创建 Exif
我正在尝试获取图像的方向(来自图库),但 ExifInterface 始终返回 0。这是代码: int orientation = exif.getAttributeInt(ExifInterface
我最近添加了 implementation 'com.google.android.play:core:1.6.4'作为我的 android 项目中的依赖项,现在 Intellij 提示 Progra
我一直在将图像上传到 FirebaseStorage,但在显示它们时,它们通常是错误的。我发现了可以确定图像方向并在必要时旋转和翻转图像的 ExifInterface。 在我的手机上从图库区域选择图像
我正在开发一个应用程序。我得到时间、日期、经度和纬度。 所有信息获取成功。 现在我想格式化从 ExifInterface.TAG_DATETIME 获得的日期和时间。 我该怎么做? 主 Activit
我在相机的 onActivityResult 上通过 bundle 传递位图。 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
我正在尝试使用 ExifInterface 更改 exif 标签。我使用 setAttribute() 并调用 saveAttributes()。标签暂时保存,下次旧值还在,没有更新.........
我正在尝试检查位图的方向并在需要时翻转它,但在应用代码时出现错误。这是我尝试使用 ExifInterface 翻转图像时的代码: @RequiresApi(api = Build.VERSION_CO
我是一名优秀的程序员,十分优秀!