0

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.stack.imgur.com/As3p4.jpg (it's a screenshot taken)

Gallery preview: https://i.stack.imgur.com/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.

Axel Syntes
  • 5
  • 1
  • 4
  • "I would like my camera preview to display the exact same image as my gallery preview" -- assuming that the "gallery preview" is some pre-installed app, this may not be possible, as device manufacturers cheat. Beyond that, you would need to determine what preview size you are using and what aspect ratio is shown by the "gallery preview". You might also need to disable full-bleed previews, as those will crop what is shown in the `CameraView`. "I think I don't understand how to use theses overrides" -- nobody can help you with those, since they are not in your question. – CommonsWare Jul 03 '15 at 10:38
  • "gallery preview is some pre-installed app" -- It's just an activity with `mImageView.setImageBitmap(myBitmap);` for know. "You might also need to disable full-bleed previews" -- To do that, getPreviewSize() must return a Size with same ratio to the one of the gallery? And I think the ratio is "aspect 4:3 (1,33 - normal)" considering the image resolution. Do you think I must override the getPreviewSize() to get a 4:3 ratrio for my Camera Preview? – Axel Syntes Jul 03 '15 at 12:10
  • "To do that, getPreviewSize()..." -- no. See [the documentation for full-bleed preview](https://github.com/commonsguy/cwac-camera#supporting-full-bleed-preview). "Do you think I must override the getPreviewSize() to get a 4:3 ratrio for my Camera Preview?" -- if I am understanding what you want (preview and picture to match), you have to choose the same aspect ratio for both the preview size and the picture size, from the eligible sizes for each. However, bear in mind that the camera does not have to return the same content for preview and pictures. – CommonsWare Jul 03 '15 at 12:43
  • Thanks Mark. I'll apply the full-bleed preview, I hope that is what I wanted. I'll keep you in touch. " you have to choose the same aspect ratio for both the preview size and the picture size " -- To do that, I must override `getPreviewSize()` and return a size with ratio of 4:3? (My image preview is 4:3) right? See [link](https://github.com/commonsguy/cwac-camera#controlling-preview-sizes)[link] – Axel Syntes Jul 03 '15 at 13:03
  • It's really weird. It's working on a Samsung Galaxy S2 (the camera preview is way wider) but not on Samsung Galaxy S3. Do you know why? My `CameraHost` is set on `false` from `useFullBleedPreview()`. In my logcat it's going through `useFullBleedPreview()` so I think it's fine there. – Axel Syntes Jul 03 '15 at 13:19
  • "Do you know why?" -- no, other than my previous note that there is no requirement that a device's camera provide the same content for previews and pictures. Hence, I have never personally worried about this problem. – CommonsWare Jul 03 '15 at 14:02

1 Answers1

0

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.

Axel Syntes
  • 5
  • 1
  • 4