[英]Camera orientation in samsung device issue
我成功點擊圖片並將其保存在卡片中。 它在所有設備上都運行良好,但問題是當我在三星設備上測試時,然后當我在縱向模式下點擊圖像時,它默認在橫向模式下保存圖像。 但我沒有做任何類型的旋轉代碼。 所以請幫助我。 我該怎么解決?
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
Uri mImageCaptureUri = null;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mImageCaptureUri = Uri.fromFile(mFileTemp);
} else {
mImageCaptureUri = InternalStorageContentProvider.CONTENT_URI;
}
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
} catch (ActivityNotFoundException e) {
Log.d("", "cannot take picture", e);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
Bitmap bitmap;
switch (requestCode) {
case REQUEST_CODE_TAKE_PICTURE:
startCropImage();
break;
case REQUEST_CODE_CROP_IMAGE:
String path = data.getStringExtra(CropImage.IMAGE_PATH);
if (path == null) {
return;
}
bitmap = BitmapFactory.decodeFile(mFileTemp.getPath());
resizedBitmap = ScalingUtilities.createScaledBitmap(bitmap, 320,
320, ScalingLogic.CROP);
imgPreview.setImageBitmap(resizedBitmap);
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void startCropImage() {
Intent intent = new Intent(this, CropImage.class);
intent.putExtra(CropImage.IMAGE_PATH, mFileTemp.getPath());
intent.putExtra(CropImage.SCALE, true);
intent.putExtra(CropImage.ASPECT_X, 2);
intent.putExtra(CropImage.ASPECT_Y, 2);
startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);
}
我在清單中添加了以下權限。
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我解決了我的錯誤,實際上我使用了一個技巧。 我像這樣以程序化的方式取得了制造的名稱
String device_name = Build.MANUFACTURER;
之后我將它保存在sharedpreferences對象中。
deviceIdPreferences = getSharedPreferences("USER_INFO", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = deviceIdPreferences.edit();
editor.putString("Device_id",deviceId);
editor.putString("device_name",device_name);
editor.commit();
之后在目標java文件中,我從sharedpreference中提取值。
preferences = this.getSharedPreferences(
"USER_INFO", Context.MODE_PRIVATE);
device_name = preferences.getString("device_name", "Empty");
然后
mBitmap = getBitmap(mImagePath);
if(device_name.equals("samsung")){
switch (Integer.parseInt(gotOrientation)) {
case ExifInterface.ORIENTATION_ROTATE_90:
mBitmap = Util.rotateImage(mBitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
mBitmap = Util.rotateImage(mBitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
mBitmap = Util.rotateImage(mBitmap, 90);
break;
default:
mBitmap = Util.rotateImage(mBitmap, 90);
break;
}
}
private Bitmap getBitmap(String path) {
Uri uri = getImageUri(path);
InputStream in = null;
try {
in = mContentResolver.openInputStream(uri);
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, o);
in.close();
int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int) Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
in = mContentResolver.openInputStream(uri);
Bitmap b = BitmapFactory.decodeStream(in, null, o2);
in.close();
return b;
} catch (FileNotFoundException e) {
Log.e(TAG, "file " + path + " not found");
} catch (IOException e) {
Log.e(TAG, "file " + path + " not found");
}
return null;
}
許多設備 - 但不是全部 - 不會保存肖像圖像。 他們將使用EXIF標題保存風景圖像,告訴圖像查看器將圖像旋轉為肖像。 並非所有圖像查看器都能處理此問題
這是我編寫CWAC-Camera庫的原因之一,它提供來自不同設備的統一攝像頭輸出。
我使用的代碼讓人想起Haresh在評論中指出的內容,以及一些值得注意的差異:
Haresh的代碼似乎假設相機處於縱向模式; 我的目標是處理這兩種情況。
Haresh的代碼在主應用程序線程上明確地完成了所有的工作,包括磁盤I / O,這是一個非常糟糕的主意。 我是一個背景線程。
他的代碼對圖像進行了下采樣。 我的猜測是,這有助於防止內存不足錯誤,這絕對是一個挑戰。 在我的例子中,我給開發人員使用我的庫指定一個大堆的選項,以幫助確保我可以執行位圖旋轉。 最后,我將此代碼移動到NDK,其中內存分配不計入Dalvik堆限制。
我的代碼還負責其他特定於設備的問題,例如在需要時翻轉FFC圖像或在開發人員請求時鏡像FFC圖像。
但是,我的代碼專門設計為我的庫使用,因此需要相當多的工作來將邏輯分解為可以獨立使用的東西。
請注意, 我的代碼的鏈接專門針對庫的v0.6.9。 如果您在寫完這個答案后幾個月正在閱讀這個類的更新版本可能存在於回購中。 相反,如果我已將代碼移動到NDK中,則repo master
可能不再存在該類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.