Android:相机的尺寸与屏幕尺寸不匹配
问题描述:
我已经编写了自己的相机活动,我在FrameLayout
中展示了该照片的实时预览,但实况图像看起来并不自然,有点高,我认为它是因为FrameLayout
的尺寸与相机尺寸不符。我应该怎么做才能解决它?Android:相机的尺寸与屏幕尺寸不匹配
这里是相机活动的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="0.86" />
<Button
android:id="@+id/button_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:gravity="center"
android:onClick="capture"
android:text="Capture" />
<Button
android:id="@+id/button_accept"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:text="Accept"
android:visibility="gone"
android:onClick = "accept" />
<Button
android:id="@+id/button_retake"
android:layout_width="107dp"
android:layout_height="wrap_content"
android:text="Retake"
android:visibility="gone"
android:onClick = "retake"/>
</LinearLayout>
答
的问题是可以解决这样的:
Parameters cameraParam = mCamera.getParameters() ;
double ratio = (double)cameraParam.getPictureSize().height/(double)cameraParam.getPictureSize().width ;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try
{
display.getSize(size);
}
catch(java.lang.NoSuchMethodError ignore)
{
size.x = display.getWidth();
size.y = display.getHeight() ;
}
int width = size.y;
int height = size.x;
previewParam.width = width;
previewParam.height = (int)(previewParam.width/ratio) ;
答
你将不得不规模已经从相机拍摄的图像。你可以尝试这样的事:
private Bitmap scaleImage(ImageView imageView, String path) {
int targetWidth = imageView.getWidth();
int targetHeight = imageView.getHeight();
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, bitmapOptions);
int photoWidth = bitmapOptions.outWidth;
int photoHeight = bitmapOptions.outHeight;
int scaleFactor = Math.min(photoWidth/targetWidth, photoHeight/targetHeight);
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inSampleSize = scaleFactor;
bitmapOptions.inPurgeable = true;
return BitmapFactory.decodeFile(path, bitmapOptions);
}
没有,已经从照相机拍摄的照片是好的,只是现场图片在相机预览中显示(拍摄照片之前)有点高! – Navid777 2013-02-25 12:11:00
是你的代码打开相机类似于: 意图cameraIntent =新的意图(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); getActivity()。startActivityForResult(cameraIntent,IMAGE_CAPTURE_REQUEST_CODE); – lokoko 2013-02-25 12:25:34
不,我写了我自己的相机活动 – Navid777 2013-02-25 12:37:22