• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Overlay on SurfaceView camera 2 api android?

I am trying to overlay a rectangle on the surfaceview that lies on top of my camera preview.However, the rectangle appears not exactly on my surfaceview but rather it appears on the background. I am kinda lost as what I could be doing wrong. I was following this link and tried it but it dint work in my case.android Camera2 API + TextureView overlay for drawing on camera preview

Fragment class
@override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view =inflater.inflate(R.layout.fragment_camera2_basic, container, false);

LinearLayout lv = (LinearLayout) view.findViewById(R.id.surface);
lv.addView(new OverlayTextureView(getActivity()));

return view;



}
OverlayTextureView class
import android.app.Fragment;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;

/**
* Created by sqripter on 23/01/18.
*/

public class OverlayTextureView extends SurfaceView {

private final Paint paint;
private final SurfaceHolder mHolder;
private final Context context;

public OverlayTextureView(Context context) {
this(context,null);
setWillNotDraw(false);
}
public OverlayTextureView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

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

mHolder = getHolder();
mHolder.setFormat(PixelFormat.TRANSPARENT);
this.context = context;
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
}

@override
protected void onDraw(Canvas canvas) {
Log.d("touch", "touchRecieved by camera");

// super.onDraw(canvas);
setWillNotDraw(false);
canvas = mHolder.lockCanvas();
paint.setStyle(Paint.Style.FILL);

int x = canvas.getWidth();
int y = canvas.getHeight();


paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
Rect rect = new Rect(50, 50, x - 300, y - 300);
canvas.drawRect(rect, paint);

mHolder.unlockCanvasAndPost(canvas);


}



}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:id="@+id/vwMainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">

<TextView
android:id="@+id/putstuffs"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_alignParentStart="true"
android:layout_marginBottom="14dp"
android:text="Timer"
android:textColor="#ff0000" />

<LinearLayout
android:id="@+id/surface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >



</LinearLayout>

<com.example.trialapp.OverlayTextureView
android:id="@+id/overlay"
android:layout_width="480dp"
android:layout_height="640dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/putstuffs"

/>




<TextureView
android:id="@+id/texture"
android:layout_width="480dp"
android:layout_height="640dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/putstuffs" />
</RelativeLayout>


<Button
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@String/picture" />




</RelativeLayout>

k6lfX.jpg
 
Back
Top Bottom