[英]Android camera taking landscape picture while on portrait orientation
我从这里采用了代码。
基本上,我在相机中间添加了一个边框以保存图片的一部分。 当相机处于风景模式时,它可以完美地拍摄照片。 我正在打电话
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的摄像头活动来调用它
// 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;
}
}
};
请帮忙。
谢谢,Noorul
编辑:
下面是该方案的说明。 边框区域是我要捕获的部分。 这是我计算边界框的边界的方法
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);
尝试下面的代码,拍照后放下面的代码:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.