捕获的图像始终显示横向并且 setRotate 无法解析
Posted
技术标签:
【中文标题】捕获的图像始终显示横向并且 setRotate 无法解析【英文标题】:Captured image always shows landscape and setRotate cannot be resolved 【发布时间】:2016-02-07 02:08:01 【问题描述】:这里有很多类似的主题和问题,我关注this。但我得到了错误。
我的代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
if (requestCode == 1)
//h=0;
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles())
if (temp.getName().equals("temp.jpg"))
f = temp;
File photo = new File(Environment.getExternalStorageDirectory(), "temp.jpg");
//pic = photo;
break;
try
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
bitmapOptions.inDither = true;
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
Global.img = bitmap;
b.setImageBitmap(bitmap);
String path = android.os.Environment.getExternalStorageDirectory() + File.separator + "Phoenix" + File.separator + "default";
//p = path;
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
try
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
//pic=file;
outFile.flush();
outFile.close();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
catch (Exception e)
e.printStackTrace();
catch (Exception e)
e.printStackTrace();
else if (requestCode == 2)
Uri selectedImage = data.getData();
// h=1;
//imgui = selectedImage;
String[] filePath = MediaStore.Images.Media.DATA;
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.w("path of image ******", picturePath + "");
b.setImageBitmap(thumbnail);
else
finish();
按照教程完成后,我已将代码更改为
try
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
bitmapOptions.inDither = true;
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath(), opts);
ExifInterface exif = new ExifInterface(f.getAbsolutePath());
String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;
Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bitmapOptions.outWidth, bitmapOptions.outHeight, matrix, true);
Global.img = bitmap;
b.setImageBitmap(bitmap);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
//p = path;
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
-
matrix.setRotate 中的 setRotate 无法解决。
(bm, 0, 0, bitmapOptions.outWidth, bitmapOptions.outHeight, matrix, true);
下方出现一条红线(无法解析方法 createBitmap)
*********编辑*********
在我导入 android.graphics.Matrix
而不是 android.opengl.Matrix,
后,应用程序崩溃了。
LogCat 错误
Process: com.example.project.project, PID: 13045
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:928)
at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
at android.graphics.Bitmap.createBitmap(Bitmap.java:833)
at com.example.project.project.ImageFitScreen.onActivityResult(ImageFitScreen.java:236)
at android.app.Activity.dispatchActivityResult(Activity.java:5643)
这是第 236 行
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bitmapOptions.outWidth, bitmapOptions.outHeight, matrix, true);
【问题讨论】:
【参考方案1】:改成这个Bitmap rotateBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(),matrix, true);
【讨论】:
我仍然遇到同样的错误。之间,变量rotatedBitmap
从未使用过。我按照教程进行操作,但没有显示应该在哪里分配 rotatedBitmap
如果你想解决你的旋转问题,你需要使用你在代码中分配的rotatedBitmap
接下来我应该写什么?如何使用?
Global.img = rotateBitmap ; b.setImageBitmap(rotatedBitmap)
仍然遇到同样的错误11-06 12:18:12.836 1874-1874/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.project.project, PID: 1874 java.lang.OutOfMemoryError at android.graphics.Bitmap.nativeCreate(Native Method) at android.graphics.Bitmap.createBitmap(Bitmap.java:928) at android.graphics.Bitmap.createBitmap(Bitmap.java:901) at android.graphics.Bitmap.createBitmap(Bitmap.java:833) at com.example.project.project.ImageFitScreen.onActivityResult(ImageFitScreen.java:236)
【参考方案2】:
我猜你导入了错误的 Matrix 类,你应该导入 android.graphics.Matrix,而不是 android.opengl.Matrix,请仔细检查。
【讨论】:
以上是关于捕获的图像始终显示横向并且 setRotate 无法解析的主要内容,如果未能解决你的问题,请参考以下文章
PHAssetChangeRequest 失败,除非原始图像方向为横向