我在横向模式下在FrameLayout中打开android Camera,然后屏幕看起来像 -
但是当我在FrameLayout中打开ImageView时,同样的Image,它看起来像 -
请忽略图片内容和第二张图片中绘制的有趣方块。 不同的是, 第一台CameraView在整个屏幕上打开或高度触及屏幕的头部,但第二张图像使图像缩小以适应。
我希望第一张图像像第二张一样工作,它应该适合定义的矩形。唯一的目标是Camera View和ImageView显示相同的Look。
如果需要,我也可以更改整个布局。 FacePreviewImageView是我要添加的图像。
First Camera View仅限Android Camera,我将添加到android framelayout -
Camera mCamera = Camera.open(1);
FrameLayout layout = (FrameLayout) findViewById(R.id.ll2);
layout.addView(new CameraView());
第二帧我正在添加 -
layout.removeAllViews();
layout.addView(faceImageView);
faceView.setVisibility(View.GONE);
faceImageView.setVisibility(View.VISIBLE);
布局XML是 -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
</LinearLayout>
<FrameLayout
android:id="@+id/ll2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="horizontal" >
<com.example.defaultfacetracker.FacePreviewImageView
android:id="@+id/facePreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@android:drawable/toast_frame"
/>
</FrameLayout>
<LinearLayout
android:id="@+id/ll3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:text="Process" />
</LinearLayout>
</LinearLayout>
我的SurfaceHolder看起来像 -
class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;
Camera.PreviewCallback previewCallback;
Preview(Context context, Camera.PreviewCallback previewCallback, Camera mCamera2) {
super(context);
this.previewCallback = previewCallback;
this.mCamera = mCamera2;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where
// to draw.
// mCamera = Camera.open(1);
//mCamera.setDisplayOrientation(270);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
// TODO: add more exception handling logic here
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the preview.
// Because the CameraDevice object is not a shared resource, it's very
// important to release it when the activity is paused.
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.05;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
for (Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Now that the size is known, set up the camera parameters and begin
// the preview.
Camera.Parameters parameters = mCamera.getParameters();
List<Size> sizes = parameters.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(sizes, w, h);
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
mCamera.setParameters(parameters);
if (previewCallback != null) {
mCamera.setPreviewCallbackWithBuffer(previewCallback);
Camera.Size size = parameters.getPreviewSize();
byte[] data = new byte[size.width*size.height*
ImageFormat.getBitsPerPixel(parameters.getPreviewFormat())/8];
mCamera.addCallbackBuffer(data);
}
// if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// mCamera.setDisplayOrientation(90);
//
// } else {
// mCamera.setDisplayOrientation(0);
// }
mCamera.startPreview();
}
}
答案 0 :(得分:4)
您设置了相机预览尺寸的错误高度和宽度,还需要使用 setDisplayOrientation ,请在您设置高度和宽度的位置显示代码,并设置显示方向度。
您也可以看到正确初始化相机here