捕获图像总是显示景观和setRotate不能得到解决 [英] Captured image always shows landscape and setRotate cannot be resolved
问题描述
有不少类似的题目和问题在这里,我按照这个 http://stackoverflow.com/a/12933632/ 5257704 。但我得到的错误。
我的code
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,因此code,数据);
如果(结果code == RESULT_OK){
如果(要求code == 1){
// H = 0;
文件F =新的文件(Environment.getExternalStorageDirectory()的toString());
对于(temp文件:f.listFiles()){
如果(temp.getName()。等于(temp.jpg)){
F =温度;
档案照片=新的文件(Environment.getExternalStorageDirectory(),temp.jpg);
// PIC =照片;
打破;
}
}
尝试 {
点阵位图;
BitmapFactory.Options bitmapOptions =新BitmapFactory.Options();
bitmapOptions.inJustDe codeBounds = FALSE;
bitmapOptions.in preferredConfig = Bitmap.Config.RGB_565;
bitmapOptions.inDither = TRUE;
位= BitmapFactory.de codeFILE(f.getAbsolutePath(),bitmapOptions);
Global.img =位图;
b.setImageBitmap(位);
字符串路径= android.os.Environment.getExternalStorageDirectory()+文件分割符+凤凰+文件分割符+默认;
// P =路径;
f.delete();
的OutputStream不过outFile = NULL;
档案文件=新的文件(路径,将String.valueOf(System.currentTimeMillis的())+.JPG);
尝试 {
不过outFile =新的FileOutputStream(文件);
bitmap.com preSS(Bitmap.Com pressFormat.JPEG,85,不过outFile);
// PIC =文件;
outFile.flush();
outFile.close();
}赶上(FileNotFoundException异常E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}赶上(例外五){
e.printStackTrace();
}
}赶上(例外五){
e.printStackTrace();
}
}否则,如果(要求code == 2){
乌里selectedImage = data.getData();
// H = 1;
// imgui = selectedImage;
的String []文件路径= {MediaStore.Images.Media.DATA};
光标C = getContentResolver()查询(selectedImage,文件路径,NULL,NULL,NULL);
c.moveToFirst();
INT参数:columnIndex = c.getColumnIndex(文件路径[0]);
字符串picturePath = c.getString(参数:columnIndex);
c.close();
位图的缩略图=(BitmapFactory.de codeFILE(picturePath));
Log.w(形象的路径******,picturePath +);
b.setImageBitmap(缩略图);
}
}
其他
{
完();
}
}
按照教程后,我改变了我的code到
尝试{
点阵位图;
BitmapFactory.Options bitmapOptions =新BitmapFactory.Options();
bitmapOptions.inJustDe codeBounds = FALSE;
bitmapOptions.in preferredConfig = Bitmap.Config.RGB_565;
bitmapOptions.inDither = TRUE;
位= BitmapFactory.de codeFILE(f.getAbsolutePath(),bitmapOptions);
BitmapFactory.Options选择采用=新BitmapFactory.Options();
位图BM = BitmapFactory.de codeFILE(f.getAbsolutePath(),选择采用);
ExifInterface EXIF =新ExifInterface(f.getAbsolutePath());
字符串orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
INT方向= orientString!= NULL?的Integer.parseInt(orientString):ExifInterface.ORIENTATION_NORMAL;
INT rotationAngle = 0;
如果(定向== ExifInterface.ORIENTATION_ROTATE_90)rotationAngle = 90;
如果(定向== ExifInterface.ORIENTATION_ROTATE_180)rotationAngle = 180;
如果(定向== ExifInterface.ORIENTATION_ROTATE_270)rotationAngle = 270;
字模=新的Matrix();
matrix.setRotate(rotationAngle,(浮动)bm.getWidth()/ 2,(浮动)bm.getHeight()/ 2);
位图rotatedBitmap = Bitmap.createBitmap(宽多重峰,0,0,bitmapOptions.outWidth,bitmapOptions.outHeight,矩阵,真);
Global.img =位图;
b.setImageBitmap(位);
字符串路径= android.os.Environment
.getExternalStorageDirectory()
+文件分割符
+凤凰+文件分割符+默认;
// P =路径;
f.delete();
的OutputStream不过outFile = NULL;
档案文件=新的文件(路径,将String.valueOf(System.currentTimeMillis的())+.JPG);
- setRotate在matrix.setRotate不能得到解决。
- 在我得到下面
A红线(BM,0,0,bitmapOptions.outWidth,bitmapOptions.outHeight,矩阵,真);
(不能分辨方法createBitmap)
********* *********修改
我输入后 android.graphics.Matrix
而不是 android.opengl.Matrix,
应用程序崩溃。
LogCat中的错误
流程:com.example.project.project,PID:13045
java.lang.OutOfMemoryError
在android.graphics.Bitmap.nativeCreate(本机方法)
在android.graphics.Bitmap.createBitmap(Bitmap.java:928)
在android.graphics.Bitmap.createBitmap(Bitmap.java:901)
在android.graphics.Bitmap.createBitmap(Bitmap.java:833)
在com.example.project.project.ImageFitScreen.onActivityResult(ImageFitScreen.java:236)
在android.app.Activity.dispatchActivityResult(Activity.java:5643)
这是行236
位图rotatedBitmap = Bitmap.createBitmap(BM,0,0,bitmapOptions.outWidth,bitmapOptions.outHeight,矩阵,真正的);
更改该位图rotatedBitmap = Bitmap.createBitmap(BM,0,0,bm.getWidth(),bm.getHeight(),矩阵,真正的);
There are quite a few similar topics and issues around here and I follow this http://stackoverflow.com/a/12933632/5257704. But I get error.
My Code
@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();
}
}
After follow the tutorial, I have changed my code to
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");
- setRotate in matrix.setRotate cannot be solved.
- I get a red line underneath
(bm, 0, 0, bitmapOptions.outWidth, bitmapOptions.outHeight, matrix, true);
(cannot resolved method createBitmap )
*********Edit*********
After I import android.graphics.Matrix
instead of android.opengl.Matrix,
the app crashed.
LogCat error
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)
This is line 236
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bitmapOptions.outWidth, bitmapOptions.outHeight, matrix, true);
Change to this Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(),matrix, true);
这篇关于捕获图像总是显示景观和setRotate不能得到解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!