I'm trying to create a custom camera preview activity, but as my camera preview view is not matches the screen height the cam preview is being shrinked and looks bad. Can someone help me and tell me how can I fix this?
Here is how it looks:
And my code:
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 layout:
<?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>