我采用了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
中的相机活动中调用它// 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);
答案 0 :(得分:0)
尝试下面的代码,拍照后,放在下面的代码:
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;