相机预览+仅检索叠加部分

时间:2018-03-26 11:20:16

标签: android camera overlay surfaceview

我想只检索SquareImageview中的内容

enter image description here 1)相机预览 - >确定

2)显示方形叠加 - >确定

3)拍照并获取位图 - >确定

4)尝试根据我的覆盖(id mask_square) - >>仅检索位图的一部分。不行,我怎么能实现呢?

我尝试了很多东西(来自SO)

// MainActivity.java的有趣部分

 Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
    @SuppressWarnings("deprecation")
    public void onPictureTaken(byte[] data, Camera camera) {

        FileOutputStream outStream = null;
        Calendar c = Calendar.getInstance();
        File videoDirectory = new File(path);

        if (!videoDirectory.exists()) {
            videoDirectory.mkdirs();
        }

        try {
            // Write to SD Card
            outStream = new FileOutputStream(path + c.getTime().getSeconds() + ".jpg");
            outStream.write(data);
            outStream.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }

        Bitmap realImage;
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 5;

        options.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared

        options.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future


        realImage = BitmapFactory.decodeByteArray(data,0,data.length,options);
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(path + c.getTime().getSeconds()
                    + ".jpg");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            Log.d("EXIF value",
                    exif.getAttribute(ExifInterface.TAG_ORIENTATION));
            if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("1")) {
                realImage = rotate(realImage, 90);
            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("8")) {
                realImage = rotate(realImage, 90);
            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("3")) {
                realImage = rotate(realImage, 90);
            } else if (exif.getAttribute(ExifInterface.TAG_ORIENTATION)
                    .equalsIgnoreCase("0")) {
                realImage = rotate(realImage, 90);
            }
        } catch (Exception e) {

        }

       // Have to do something here to get only
       the portion of what is shown inside the overlay
    }
};

// take_photo.layout

<FrameLayout
        android:id="@+id/preview"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.view.SurfaceView
            android:id="@+id/surface"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

        <LinearLayout

        <fr.leshabitues.shop.app.utils.SquareImageView
            android:id="@+id/mask_square"
            android:layout_gravity="center"
            android:scaleType="fitCenter"
            android:background="@drawable/mask_square"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_margin="40dp"/>

    </FrameLayout>

// SquareImageView.java

public class SquareImageView extends ImageView {

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

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

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

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int width = getMeasuredWidth();
    int height = getMeasuredHeight();

    if (ViewUtils.isTablet(getContext())) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (width != height) {
            setMeasuredDimension(height, height);
        }
    } else {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        if (width != height) {
            setMeasuredDimension(widthMeasureSpec, widthMeasureSpec);
        }
    }
  }
}

// Preview.JAVA

public class Preview extends ViewGroup implements SurfaceHolder.Callback {
private final String TAG = "Preview";

SurfaceView mSurfaceView;
SurfaceHolder mHolder;
int heightmax ;
int widthmax ;
Size mPreviewSize;
List<Size> mSupportedPreviewSizes;
Camera mCamera;

@SuppressWarnings("deprecation")
public Preview(Context context, SurfaceView sv) {
    super(context);

    mSurfaceView = sv;

    mHolder = mSurfaceView.getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void setCamera(Camera camera) {
    mCamera = camera;
    if (mCamera != null) {
        mSupportedPreviewSizes = mCamera.getParameters().getSupportedPictureSizes();
        requestLayout();

        // get Camera parameters
        Camera.Parameters params = mCamera.getParameters();

        List<String> focusModes = params.getSupportedFocusModes();
        if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
            // set the focus mode
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
            // set Camera parameters
            mCamera.setParameters(params);
        }

    }
}

public void pause(){

    if (mCamera != null){
        mCamera.lock();
    }

}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
    final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
    setMeasuredDimension(width, height);

    if (mSupportedPreviewSizes != null) {
        mPreviewSize=maxSize();
    }
}

public Size maxSize(){
    Size sizeMax=mSupportedPreviewSizes.get(0);
    for(Size size:mSupportedPreviewSizes){
        if(size.height*size.width>sizeMax.width*sizeMax.height){
            sizeMax = size;
        }
    }
    return sizeMax;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if (changed && getChildCount() > 0) {
        final View child = getChildAt(0);

        final int width = r - l;
        final int height = b - t;

        int previewWidth = width;
        int previewHeight = height;
        if (mPreviewSize != null) {
            previewWidth = mPreviewSize.width;
            previewHeight = mPreviewSize.height;
        }

        // Center the child SurfaceView within the parent.
        if (width * previewHeight > height * previewWidth) {
            final int scaledChildWidth = previewWidth * height / previewHeight;
            child.layout((width - scaledChildWidth) / 2, 0,
                    (width + scaledChildWidth) / 2, height);
        } else {
            final int scaledChildHeight = previewHeight * width / previewWidth;
            child.layout(0, (height - scaledChildHeight) / 2,
                    width, (height + scaledChildHeight) / 2);
        }
    }
}

public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, acquire the camera and tell it where
    // to draw.
    try {
        if (mCamera != null) {
            mCamera.setPreviewDisplay(holder);
        }
    } catch (IOException exception) {
        Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // Surface will be destroyed when we return, so stop the preview.
    if (mCamera != null) {
        mCamera.stopPreview();
    }
}



Camera.AutoFocusCallback mnAutoFocusCallback = new Camera.AutoFocusCallback() {
    @Override
    public void onAutoFocus(boolean success, Camera camera) {
    }
};

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    if(mCamera != null) {
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPictureSize(mPreviewSize.width, mPreviewSize.height);
        requestLayout();

        mCamera.setParameters(parameters);
        mCamera.startPreview();

    }
}
}

0 个答案:

没有答案