Axel Syntes
Axel Syntes

Reputation: 5

CWAC-Camera, camera preview too small

I'm crap at english, hope it's okay.

I'm working on CWAC-camera, and there is an issue I don't understand.

I'm trying to take a picture with CWAC-camera and then displays it in a "gallery-like" activity. The thing is, the "range"(?) of my camera preview is way smaller than in my gallery preview.

I think it's clearer if I show you pictures:

Camera preview: https://i.sstatic.net/As3p4.jpg (it's a screenshot taken)

Gallery preview: https://i.sstatic.net/7iPsn.jpg

I would like my camera preview to display the exact same image as my gallery preview. And I can't make it work. As you can see, the Gallery preview is way wider, and I want this for my camera preview.

My software is using getPreferredPreviewSize(). I tried to override getPreviewSize() or getPreferredPreviewSizeForVideo() but It doesn't change anything. I think I don't understand how to use theses overrides, even with the documentation and the questions on stackoverflow.

Here is a cropped version of my MainActivity :

private CameraView mCameraView;

@Override
 public void onAttach(Activity activity) {
    super.onAttach(activity);
    MySingletonClass singleton = new MySingletonClass();

    Singleton.getInstance().setMySingletonClass(singleton);
    // We overrided the Camera Host so we can start the email activity in the saveImage() method
    MyCustomCameraHost myCustomCameraHost = new MyCustomCameraHost(activity, this, mIsSaving, mCameraView);
    this.setHost(myCustomCameraHost);
}
[...]
@Override
public void onDetach() {
    super.onDetach();
    mSensorManager.unregisterListener(this);
}
[...]
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_preview, container, false);
    view.setOnClickListener(this);

    mCameraView = (CameraView) view.findViewById(R.id.cameraSurfaceView);
    mCameraView.setHost(getHost());

    //Camera.open(getHost().getCameraId()).setOneShotPreviewCallback(this);
    mCameraView.setPreviewCallback(this);
    setCameraView(mCameraView);

    return view;
}

Here is my CameraView layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@drawable/border"
android:layout_height="match_parent" >
[...]
<com.commonsware.cwac.camera.CameraView
    android:id="@+id/cameraSurfaceView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />
[...]
</RelativeLayout>

My display activity is a simple :

mImageView.setImageBitmap(myBitmap);

My phone has a native resolution of 1280*720 so I thought it was this ratio that was taken for the camera preview. The photo taken have a resolution of 3264*2448, so when I display it in my gallery it has black borders (I don't mind).

Could you help me fix the "range" of my Camera Preview?

Thanks for your time.

Upvotes: 0

Views: 215

Answers (1)

Axel Syntes
Axel Syntes

Reputation: 5

My answer:

I overrided this: (SimpleCameraHost.java)

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public Camera.Size getPreferredPreviewSizeForVideo(int displayOrientation,
                                                 int width,
                                                 int height,
                                                 Camera.Parameters parameters,
                                                 Camera.Size deviceHint) {
List<Camera.Size> previewSizesList = parameters.getSupportedPreviewSizes();

for(int idx = 0; idx < previewSizesList.size(); idx++)
{
  Log.e("myDebug", "Height----------------- " + previewSizesList.get(idx).height);
  Log.e("myDebug", "Width----------------- " + previewSizesList.get(idx).width);
}

if (deviceHint != null) {
  return(previewSizesList.get(0));
  // return(deviceHint);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  //it goes through here
  return(previewSizesList.get(0));
  //return(parameters.getPreferredPreviewSizeForVideo());
}

return(null);
}

I choosed previewSizesList.get(0) (index 0) because the ratio of this size (960w, 720h) provided by getSupportedPreviewSizes() is 1,33333, which matches with the ratio of the photo I can take (3264/2448 = 1,3333).

You could also check the documentation here: https://github.com/commonsguy/cwac-camera#supporting-full-bleed-preview it has the same result (on some devices) but is less complicated to do.

Upvotes: 0

Related Questions