我有一项活动可以捕捉图像并将图像放在图像视图中。但是限制是我需要仅在纵向模式下放置图像,当我以横向模式拍摄图像时,这会失败。因为在横向模式下捕获图像后旋转图像。为了避免这种情况,我使用了
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
我在清单文件中更改了, 机器人:screenOrientation = “画像” 机器人:screenOrientation = “reversePortrait”
但上述事情并没有解决问题,任何人都可以在这里帮忙...
我的活动是,
public class MainActivity extends Activity {
ImageView img1;
Button but;
@Override
public void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1=(ImageView) findViewById(R.id.imageView1);
but=(Button) findViewById(R.id.button1);
but.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
takephoto();
}
});
}
public void takephoto()
{
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri pictureUri = Uri.fromFile(new
File(Environment.getExternalStorageDirectory()+"/img1.jpg"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
this.startActivityForResult(camera, 1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Bitmap
test=BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
+"/img1.jpg");
img1.setImageBitmap(test);
}
}
我的清单文件是,
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
public Bitmap getImage1(String path) throws IOException
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
int srcWidth = options.outWidth;
int srcHeight = options.outHeight;
int[] newWH = new int[2];
newWH[0] = srcWidth/2;
newWH[1] = (newWH[0]*srcHeight)/srcWidth;
int inSampleSize = 1;
while(srcWidth / 2 >= newWH[0]){
srcWidth /= 2;
srcHeight /= 2;
inSampleSize *= 2;
}
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = inSampleSize;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options);
ExifInterface exif = new ExifInterface(path);
String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION);
System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s);
Matrix matrix = new Matrix();
float rotation = rotationForImage(con, Uri.fromFile(new File(path)));
if (rotation != 0f) {
matrix.preRotate(rotation);
}
Bitmap pqr=Bitmap.createBitmap(
sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(), sampledSrcBitmap.getHeight(), matrix, true);
return pqr;
}
public float rotationForImage(Context context, Uri uri) {
if (uri.getScheme().equals("content")) {
String[] projection = { Images.ImageColumns.ORIENTATION };
Cursor c = context.getContentResolver().query(
uri, projection, null, null, null);
if (c.moveToFirst()) {
return c.getInt(0);
}
} else if (uri.getScheme().equals("file")) {
try {
ExifInterface exif = new ExifInterface(uri.getPath());
int rotation = (int)exifOrientationToDegrees(
exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL));
return rotation;
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
return 0f;
}
private static float exifOrientationToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
}
return 0;
}
将这些功能添加到onActivityresult
中的活动中 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
Bitmap
test=getImage1(Environment.getExternalStorageDirectory()+"/img1.jpg");
img1.setImageBitmap(test);
}