[英]onActivityResult with ImageView
我有一種拍照的方法,按照我想要的方式拍攝垂直形式的樣本,水平圈並保存垂直照片,就像我希望的練習練習一樣,名為 ViewerActivity 的活動,它是這里問題開始的地方在 ImageView 中顯示照片水平樣本的時刻:/他正在閱讀並且有類 ExifInterface 允許控制圖像的方向,但在顯示圖像的時刻仍然是水平的
這個班級負責發送照片。
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
try {
ExifInterface exifInterface = new ExifInterface(photoFile.getAbsolutePath());
int valor = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (valor) {
case ExifInterface.ORIENTATION_ROTATE_90:
orientation = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
orientation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
orientation = 270;
break;
}
} catch (Exception e) {
Log.d("mensaje", e.toString());
}
Intent i = new Intent(this, ViewerActivity.class);
i.putExtra(EXTRA_PHOTO_PATH, photoFile.getAbsolutePath());
i.putExtra(EXTRA_PHOTO_ORIENTATION, orientation);
i.putExtra(EXTRA_PHOTO_EDIT, false);
startActivityForResult(i, 10);
這個 ViewerActivity 類顯示照片然后發送它
private void sendPicture() {
Intent intent = new Intent();
intent.putExtra("path", localPath);
intent.putExtra("orientation",orientation);
setResult(Activity.RESULT_OK, intent);
finish();
}
使用RotateLayout
,完整的類和示例可在以下鏈接中找到。
https://github.com/rongi/rotate-layout
與使用Matrix
制作新的旋轉Bitmap
相比,使用RotateLayout
非常容易並且內存消耗更少。
將您的ImageView
放在RotateLayout
如下面的代碼
<com.github.rongi.rotate_layout.layout.RotateLayout
android:id="@+id/form3_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:angle="180">
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</com.github.rongi.rotate_layout.layout.RotateLayout>
並設置orientation
值,是通過讓ExifInterface
成angle
物業RotateLayout
。而ImageView
旋轉,你不需要擔心的位圖或其他任何東西ExifInterface
orientation
。
您可以從 java 代碼動態設置角度值,到RotateLayout
對象中,如下所示
rotateLayout.setAngle(newAngle);
嘗試這個:
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);
ExifInterface exif = new ExifInterface(file);
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, bounds.outWidth, bounds.outHeight, matrix, true);`
其中file
是圖像文件的名稱。
try {
File f = new File(imagePath);
ExifInterface exif = new ExifInterface(f.getPath());
int orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
int angle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}
Matrix mat = new Matrix();
mat.postRotate(angle);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
null, options);
bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), mat, true);
ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100,
outstudentstreamOutputStream);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
Log.w("TAG", "-- Error in setting image");
} catch (OutOfMemoryError oom) {
Log.w("TAG", "-- OOM Error in setting image");
}
當您在 onActivityResult 中獲取圖像路徑時嘗試此操作
嘗試一下
public Bitmap rotateImage() {
Bitmap scaled = null;
try {
FileInputStream fis = new FileInputStream(file);
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
Bitmap bm = BitmapFactory.decodeStream(fis);
ExifInterface exif = new ExifInterface(file.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, bm.getWidth(), bm.getHeight(), matrix, true);
int height = rotatedBitmap.getHeight(), scaledheight, scaledwidth;
int width = rotatedBitmap.getWidth();
float aspect;
if (width < height) {// portrait
aspect = ((float) height / (float) width);
scaledwidth = 400;
scaledheight = (int) (400 * aspect);
} else {// landscape
aspect = ((float) width / (float) height);
scaledheight = 400;
scaledwidth = (int) (400 * aspect);
}
scaled = Bitmap.createScaledBitmap(rotatedBitmap, scaledwidth, scaledheight, false);
File f = new File(getCacheDir(), "scaledBitmap");
f.createNewFile();
Bitmap bitmap = scaled;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
file = f;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("photofile io error", "EXif not done");
}
return scaled;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.