Android SurfaceView与保存的图像不同
我正在创建一个自定义相机,在它完成之前,我还有两个bug需要修复。我在这里向他们中的一个寻求帮助 我正在尝试创建一个正方形预览(SurfaceView)并将该正方形保存到文件中。我能够实现这一点的唯一方法是使用自定义视图,如下所示:Android SurfaceView与保存的图像不同,android,android-camera,surfaceview,Android,Android Camera,Surfaceview,我正在创建一个自定义相机,在它完成之前,我还有两个bug需要修复。我在这里向他们中的一个寻求帮助 我正在尝试创建一个正方形预览(SurfaceView)并将该正方形保存到文件中。我能够实现这一点的唯一方法是使用自定义视图,如下所示: public class SquareSurfaceView extends SurfaceView { private int width; private int height; public SquareSurfaceView(Co
public class SquareSurfaceView extends SurfaceView {
private int width;
private int height;
public SquareSurfaceView(Context context) {
super(context);
}
public SquareSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareSurfaceView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(width, width);
}
public int getViewWidth() {
return width;
}
public int getViewHeight() {
return height;
}
}
我遇到的问题是预览图像和保存的图像有点不一致。保存的图像从图像顶部显示更多内容。。比如说,我拍了一张门的照片,我将预览的顶部与门的顶部对齐,拍下照片,保存的图像,显示了我认为大约30 dp的更多图像,显示了预览没有显示的门上方的东西
我用来裁剪图像的代码如下:
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyyMMdd_HHmmss", Locale.US);
mLastPhotoFilename = "IMG_" + sdf.format(new Date())
+ ".jpg";
// cropped file
File photoFile = new File(photoStoragePath,
mLastPhotoFilename);
// gallery file
File galleryFile = new File(galleryStoragePath,
mLastPhotoFilename);
// write the cropped file
mLastPhoto
.compress(
CompressFormat.JPEG,
95,
new FileOutputStream(galleryFile
.getAbsolutePath()));
// resize the bitmap
Bitmap scaledPhoto = Bitmap.createBitmap(mLastPhoto, 0, 0,
mLastPhoto.getWidth(), mLastPhoto.getWidth());
// write the cropped file
scaledPhoto.compress(CompressFormat.JPEG, 95,
new FileOutputStream(photoFile.getAbsolutePath()));
我将完整图像保存到phones gallery,调整图像大小,并将调整后的图像存储到my apps目录
那么,为什么我的预览没有显示相机看到的内容,并且正在保存?我如何才能获得预览的顶部,成为相机顶部所看到的