gpt4 book ai didi

java - 如何在 Android 中旋转位图?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:59:42 24 4
gpt4 key购买 nike

我知道已经有关于这个问题的线索,但解决方案似乎使用了 Matrix 类中的方法,但这些方法似乎不再有效。即使在导入之后,方法也无法解析。我基本上是在尝试将位图旋转 90 度,因为当我垂直拍照时它会横向出现。这是我的 Activity 代码:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);

}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
//Check that request code matches ours:
if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE)
{
//Get our saved file into a bitmap object:
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);

Intent intent = new Intent(this, EditActivity.class);

ByteArrayOutputStream bs = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
intent.putExtra("byteArray", bs.toByteArray());

startActivity(intent);



}
}

最佳答案

试试这个:

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

请在这里传递您的位图或您想要显示位图的角度,例如 90 180 等。它将使用 Matrix 类的 postRotate() 方法更改位图屏幕,并再次创建位图并还原您

关于java - 如何在 Android 中旋转位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29982528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com