1

I am creating a custom camera, in which I have 2 more bugs to fix before its finished. I'm here seeking help with 1 of them.

I am trying to create a square preview (SurfaceView) and save that square to file. The only way I've been able to accomplish this is with a custom View, seen here:

public class SquareSurfaceView extends SurfaceView {

    private int width;
    private int height;

    public SquareSurfaceView(Context context) {
        super(context);
    }

    public SquareSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareSurfaceView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(width, width);
    }

    public int getViewWidth() {
        return width;
    }

    public int getViewHeight() {
        return height;
    }
}

The problem I'm having is that the image on the preview and the saved image are a bit off. The saved image shows more from the top of the image.. So, lets say I take a picture of a door, and I align the top of the preview with the top of the door, take the photo, the image that gets saved, shows what I would equate to be about 30dp MORE image, showing stuff above the door that the preview didnt show.

The code I used to crop the image is below:

SimpleDateFormat sdf = new SimpleDateFormat(
        "yyyyMMdd_HHmmss", Locale.US);
mLastPhotoFilename = "IMG_" + sdf.format(new Date())
        + ".jpg";

// cropped file
File photoFile = new File(photoStoragePath,
        mLastPhotoFilename);

// gallery file
File galleryFile = new File(galleryStoragePath,
        mLastPhotoFilename);

// write the cropped file
mLastPhoto
        .compress(
                CompressFormat.JPEG,
                95,
                new FileOutputStream(galleryFile
                        .getAbsolutePath()));

// resize the bitmap
Bitmap scaledPhoto = Bitmap.createBitmap(mLastPhoto, 0, 0,
        mLastPhoto.getWidth(), mLastPhoto.getWidth());

// write the cropped file
scaledPhoto.compress(CompressFormat.JPEG, 95,
        new FileOutputStream(photoFile.getAbsolutePath()));

I save the full image to the phones gallery, resize the image, and store the resized to my apps directory.

So, why does my preview not show what the camera sees, and is saving? How can I get the top of my preview, to be what the top of the camera is seeing?

Rabbott
  • 4,282
  • 1
  • 30
  • 53

0 Answers0