gpt4 book ai didi

java - 在android中从前置摄像头和后置摄像头拍摄图像后如何修复图像旋转度?

转载 作者:行者123 更新时间:2023-12-04 10:39:53 25 4
gpt4 key购买 nike

从相机拍摄图像后,此图像在前置摄像头和后置摄像头之间以不同的角度旋转我尝试在从相机拍摄图像后向图像添加旋转,但它不适用于所有设备的相同程度。

备注 : 我添加了人脸识别,但是当图像旋转时它不起作用,这是我的代码。

public class CheckIn extends AppCompatActivity  {
private ImageView camera;
String encoded = "";
byte[] byteArray;
static final int REQUEST_IMAGE_CAPTURE = 1;
private Button checkIn;
ContentValues values;
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
Uri imageUri;
Bitmap thumbnail;
SparseArray<Face> faces ;
Matrix matrix ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_in);
camera = (ImageView) findViewById(R.id.camera);
checkIn = (Button) findViewById(R.id.submit);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
});

checkPermissionREAD_EXTERNAL_STORAGE(this);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

try {
thumbnail = MediaStore.Images.Media.getBitmap(
getContentResolver(), imageUri);

if(Build.VERSION.SDK_INT >= 27) {
//only api 27 above
matrix = new Matrix();
matrix.postRotate(90);
}else{
//only api 27 down
matrix = new Matrix();
matrix.postRotate(270);
}

Bitmap rotatedBitmap = Bitmap.createBitmap(thumbnail, 0, 0, thumbnail.getWidth(), thumbnail.getHeight(), matrix, true);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Bitmap converetdImage = getResizedBitmap(rotatedBitmap, 700);
converetdImage.compress(Bitmap.CompressFormat.PNG, 10, byteArrayOutputStream);
byteArray = byteArrayOutputStream.toByteArray();
encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.mipmap.ic_launcher_round)
.error(R.mipmap.ic_launcher_round);

Paint myRectPaint = new Paint();
myRectPaint.setStrokeWidth(5);
myRectPaint.setColor(Color.RED);
myRectPaint.setStyle(Paint.Style.STROKE);

Bitmap tempBitmap = Bitmap.createBitmap(rotatedBitmap.getWidth(), rotatedBitmap.getHeight(),
Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
tempCanvas.drawBitmap(rotatedBitmap, 0, 0, null);
FaceDetector faceDetector
= new
FaceDetector.Builder(getApplicationContext()).setTrackingEnabled(false)
.build();

Frame frame = new Frame.Builder().setBitmap(rotatedBitmap).build();
faces = faceDetector.detect(frame);

for (int i = 0; i < faces.size(); i++) {
Face thisFace = faces.valueAt(i);
float x1 = thisFace.getPosition().x;
float y1 = thisFace.getPosition().y;
float x2 = x1 + thisFace.getWidth();
float y2 = y1 + thisFace.getHeight();
tempCanvas.drawRoundRect(new RectF(x1, y1, x2, y2), 2, 2, myRectPaint);

}
Glide.with(CheckIn.this).load(tempBitmap).apply(options)
.into(camera);

} catch (Exception e) {
e.printStackTrace();
} }
}

public void checkPermissionREAD_EXTERNAL_STORAGE(
final Context context) {
// Enable if permission granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED) {
}
// Else ask for permission
else {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();

float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
}

提前致谢 。

最佳答案

请试试这个

我只需要添加
camera.setDisplayOrientation(90);
现在显示在正确的角度。

关于java - 在android中从前置摄像头和后置摄像头拍摄图像后如何修复图像旋转度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59982243/

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