gpt4 book ai didi

java - Android 相机在纵向方向上拍摄横向照片

转载 作者:太空宇宙 更新时间:2023-11-04 14:52:20 26 4
gpt4 key购买 nike

我采用了here中的代码。

基本上,我在相机中间添加一个边界框来保存图片的部分。当相机处于横向模式时,它可以完美地拍摄照片。我正在打电话

mCamera.setDisplayOrientation(90);

方法和预览以纵向模式显示,并且边界框也正确对齐,但拍照时,它会拍摄与边界框成 90 度的区域的照片。

public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where to draw.
try {
mCamera = Camera.open();
mCamera.setDisplayOrientation(90);
}
catch (RuntimeException exception) {
//Log.i(TAG, "Exception on Camera.open(): " + exception.toString());
Toast.makeText(getContext(), "Camera broken, quitting :(",Toast.LENGTH_LONG).show();
// TODO: exit program
}

该方法需要获取图片

public Bitmap getPic(int x, int y, int width, int height) {
System.gc();
Bitmap b = null;
Size s = mParameters.getPreviewSize();

YuvImage yuvimage = new YuvImage(mBuffer, ImageFormat.NV21, s.width, s.height, null);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(x, y, width, height), 100, outStream); // make JPG
b = BitmapFactory.decodeByteArray(outStream.toByteArray(), 0, outStream.size()); // decode JPG
if (b != null) {
//Log.i(TAG, "getPic() WxH:" + b.getWidth() + "x" + b.getHeight());
} else {
//Log.i(TAG, "getPic(): Bitmap is null..");
}
yuvimage = null;
outStream = null;
System.gc();
return b;
}

我从按钮的 onClickListener 的相机 Activity 中调用它

// This method takes the preview image, grabs the rectangular
// part of the image selected by the bounding box and saves it.
// A thread is needed to save the picture so not to hold the UI thread.
private OnClickListener previewListener = new OnClickListener() {

@Override
public void onClick(View v) {
if (mAutoFocus){
mAutoFocus = false;
//mPreview.setCameraFocus(myAutoFocusCallback);
Wait.oneSec();
Thread tGetPic = new Thread( new Runnable() {
public void run() {
Double[] ratio = getRatio();
int left = (int) (ratio[1]*(double)mView.getmLeftTopPosX());
// 0 is height
int top = (int) (ratio[0]*(double)mView.getmLeftTopPosY());
int right = (int)(ratio[1]*(double)mView.getmRightBottomPosX());
int bottom = (int)(ratio[0]*(double)mView.getmRightBottomPosY());
Message msg = Message.obtain();
try {
savePhoto(mPreview.getPic(left,top,right,bottom));
mAutoFocus = true;
msg.arg1 = 1;
}
catch (Exception e) {
Log.w("SAV_JPG",e.getMessage());
msg.arg1 = -1;
}
saveImageHandler.sendMessage(msg);
}
});
tGetPic.start();

}
boolean pressed = false;
if (!mTakePicture.isPressed()){
pressed = true;
}
}
};

请提供任何帮助。

谢谢,努鲁

编辑:

下面是该场景的说明。边界框区域是我要捕获的部分。这是我计算边界框的边界的方法

Display display = wm.getDefaultDisplay();
Point size = new Point();
int width = display.getWidth();
int height = display.getHeight();
mLeftTopPosY = Math.round(height / 2) - 100;
mLeftTopPosX = Math.round(width / 2) - 250;
mRightTopPosY = Math.round(height / 2) - 100;
mRightTopPosX = Math.round(width / 2) + 250;
mLeftBottomPosY = Math.round(height / 2) + 100;
mLeftBottomPosX = Math.round(width / 2) - 250;
mRightBottomPosY = Math.round(height / 2) + 100;
mRightBottomPosX = Math.round(width / 2) + 250;

mCenter = mLeftTopIcon.getMinimumHeight()/2;
mLeftTopIcon.setBounds((int)mLeftTopPosX, (int)mLeftTopPosY,
mLeftTopIcon.getIntrinsicWidth()+(int)mLeftTopPosX,
mLeftTopIcon.getIntrinsicHeight()+(int)mLeftTopPosY);

mRightTopIcon = context.getResources().getDrawable(R.drawable.corners);
mRightTopIcon.setBounds((int)mRightTopPosX, (int)mRightTopPosY,
mRightTopIcon.getIntrinsicWidth()+(int)mRightTopPosX,
mRightTopIcon.getIntrinsicHeight()+(int)mRightTopPosY);

mLeftBottomIcon = context.getResources().getDrawable(R.drawable.corners);
mLeftBottomIcon.setBounds((int)mLeftBottomPosX, (int)mLeftBottomPosY,
mLeftBottomIcon.getIntrinsicWidth()+(int)mLeftBottomPosX,
mLeftBottomIcon.getIntrinsicHeight()+(int)mLeftBottomPosY);

mRightBottomIcon = context.getResources().getDrawable(R.drawable.corners);
mRightBottomIcon.setBounds((int)mRightBottomPosX, (int)mRightBottomPosY,
mRightBottomIcon.getIntrinsicWidth()+(int)mRightBottomPosX,
mRightBottomIcon.getIntrinsicHeight()+(int)mRightBottomPosY);

Scenario Illustration

最佳答案

尝试下面的代码,拍照后,输入下面的代码:

File f = new File(filePath);
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;

关于java - Android 相机在纵向方向上拍摄横向照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23646506/

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