我正在尝试创建自定义相机预览活动,但由于我的相机预览视图与屏幕高度不匹配,因此凸轮预览会缩小并且看起来很糟糕。有人可以帮助我并告诉我如何解决这个问题?
以下是它的外观:
我的代码:
public class CaptureFly extends Activity implements PictureCallback {
private ImageButton takePicture;
private ToggleButton flashOnOff;
private Camera camera;
private CameraPreview cameraPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.take_photo);
init();
}
private void init() {
flashOnOff = (ToggleButton) findViewById(R.id.capture_flash);
takePicture = (ImageButton) findViewById(R.id.capture_btn);
camera = CameraHelper.getCameraInstance();
if (CameraHelper.cameraAvailable(camera)) {
initCameraPreview();
}
flashOnOff.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// stopPreview();
}
});
takePicture.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
camera.takePicture(null, null, CaptureFly.this);
}
});
initActionbar();
}
// Show the camera view on the activity
private void initCameraPreview() {
cameraPreview = (CameraPreview) findViewById(R.id.camera_preview);
cameraPreview.init(camera);
}
private static String savePictureToFileSystem(byte[] data) {
File file = MediaHelper.getOutputMediaFile();
MediaHelper.saveToFile(data, file);
return file.getAbsolutePath();
}
private void setResult(String path) {
Intent intent = new Intent(CaptureFly.this, FlyPreview.class);
intent.putExtra(Constants.EXTRA_IMAGE_PATH, path);
startActivity(intent);
}
// ALWAYS remember to release the camera when you are finished
@Override
protected void onPause() {
super.onPause();
releaseCamera();
}
private void releaseCamera() {
if (camera != null) {
camera.release();
camera = null;
}
}
private void initActionbar() {
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.nav_bg));
// adds a back button
getActionBar().setHomeButtonEnabled(true);
getActionBar().setIcon(R.drawable.exit_ex);
int titleId = getResources().getIdentifier("action_bar_title", "id",
"android");
TextView abTitle = (TextView) findViewById(titleId);
Typeface mTypeface = Typeface.createFromAsset(getAssets(),
"rockwell.ttf");
abTitle.setPadding(20, 0, 0, 0);
abTitle.setTypeface(mTypeface);
abTitle.setText(getString(R.string.capture_an_image));
abTitle.setTextSize(20);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.v("--", "Picture taken");
String path = savePictureToFileSystem(data);
setResult(path);
finish();
}
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="vertical"
android:weightSum="11" >
<com.flyflasher.views.CameraPreview
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/nav_bg"
android:orientation="horizontal" >
<Button
android:id="@+id/capture_browse"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:drawableLeft="@drawable/browse"
android:gravity="left|center_vertical"
android:padding="5dp"
android:text="@string/browse_" />
<ToggleButton
android:id="@+id/capture_flash"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:drawableLeft="@drawable/flash_on"
android:gravity="left|center_vertical"
android:textOff="@string/flash_off"
android:textOn="@string/flash_on" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:background="#272721"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/capture_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:contentDescription="@string/app_name"
android:src="@drawable/capture_btn" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
您需要通过callind camera.setDisplayOrientation方法设置摄像机的显示方向。你可以在android docs中找到详细信息。 这样做还会根据屏幕宽高比和相机预览尺寸之间的差异显示一点挤压。因此,您可能需要相应地调整SurfaceView大小。更多信息here