我有以下问题:我有一个带摄像头的应用程序及其顶部和图像作为叠加。图像是4/3我得到了我的屏幕的最佳尺寸,我画了图像。 问题是我在尝试时看不到所有图像。我将图像切割在屏幕的顶部和底部。问题是,如果我拍照,那么我看到照片完美
我正在使用android sdk 8,相机处于横向模式。在我的三星Galaxy S中,叠加和surfacepreview给出相同的尺寸640:480。
我已经玩了一段时间了,但我还没有找到解决方案。
这是代码。
我的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/framecamera"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:padding="0dip"
android:layout_margin="0dip"
>
<FrameLayout
android:id="@+id/surface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dip"
android:layout_margin="0dip"
>
</FrameLayout>
<ImageButton
android:id="@+id/button_capture"
android:contentDescription="@string/click"
android:text="@string/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:background="@drawable/camera"
android:src="@drawable/camera"
/>
</LinearLayout>
我的代码的重要部分:
setContentView(R.layout.camera);
//create an instance of Camera
mcam = getCameraInstance();
Camera.Parameters parameters=mcam.getParameters();
size=getBestPreviewSize(parameters);//gets the 4/3 size
horizontal = Bitmap.createScaledBitmap(horizontal, size.width, size.height, false);
// Create our Preview view and set it as the content of our activity.
mpreview = new CameraPreview(getBaseContext(), mcam);
FrameLayout frame = (FrameLayout) findViewById(R.id.surface);
SurfaceHolder mSurfaceHolder = mpreview.getHolder();
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//set our view to 4:3
ViewGroup.LayoutParams params = frame.getLayoutParams();
params.width = size.width;
params.height = size.height;
frame.setLayoutParams(params);
//add the necessary margin to the view
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
MarginLayoutParams ps=(MarginLayoutParams )frame.getLayoutParams();
ps.topMargin = 0;
ps.bottomMargin = 0;
ps.leftMargin = (display.getWidth()-size.width)/2;//to center the overlay
frame.setLayoutParams(ps);
frame.addView(mpreview);
over = new OverlayPreview(getBaseContext(),horizontal);//inside I draw the canvas
frame.addView(over);
任何想法都将不胜感激。我认为问题来自填充可能或xml,但我不知道如何解决它。非常感谢你。
p.d:抱歉我的英语,而不是我的第一语言。
答案 0 :(得分:0)
OK!我终于找到了怎么做!
我改变了方法,现在我正在使用ImageView作为框架。
// Create our Preview view
mpreview = new CameraPreview(getBaseContext(), mcam);
FrameLayout frame = (FrameLayout) findViewById(R.id.surface);
SurfaceHolder mSurfaceHolder = mpreview.getHolder();
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
ImageView imageframe = (ImageView) findViewById(R.id.iframe);
imageframe.setImageBitmap(horizontal);
//set our view to 4:3
ViewGroup.LayoutParams params = frame.getLayoutParams();
params.width = size.width;
params.height = size.height;
frame.setLayoutParams(params);
//add the necessary margin to the view
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
MarginLayoutParams ps=(MarginLayoutParams)frame.getLayoutParams();
ps.topMargin = 0;
ps.bottomMargin = 0;
ps.leftMargin = (int)((display.getWidth()-size.width)/2) + 50;
ps.rightMargin = (int)((display.getWidth()-size.width)/2) + 50;
frame.setLayoutParams(ps);
frame.addView(mpreview);
我想这不是最好的解决方案,但它至少有效。