Heixss
Heixss

Reputation: 303

How to make a camera preview transparent?

I've seen many post about camera preview with a transparent image/button ABOVE it, but I want to know how to make a semi transparent camera preview, I tried to make the surfaceview transparent with #800000 in the xml file, but it has no effect over the camera preview.. I'm trying to make an app similar to Transparent screen. Here is a service I made:

public class FlyingCamera extends Service implements SurfaceHolder.Callback{
    SurfaceView view;
    SurfaceHolder holder;
    Camera c;
    View child;
    LayoutInflater inflater;
    Context context;
    public Camera camera=null;


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();


    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
             PixelFormat.TRANSLUCENT);
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

    View myView = inflater.inflate(R.layout.camera_surface, null);
    view = (SurfaceView) myView.findViewById(R.id.surfaceView1);

    holder=view.getHolder();

    holder.addCallback(this);

    myView.setOnTouchListener(new OnTouchListener() {
           @Override
           public boolean onTouch(View v, MotionEvent event) {
               Log.d("Touch", "touch me");
           return false;
           }
         });
    // Add layout to window manager

    wm.addView(myView, params);

}





@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub

    return null;

}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    stopSelf();
    super.onDestroy();
    Log.d("Destroy","God Help DESTROY");
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera=Camera.open();

    try{
        camera.setPreviewDisplay(holder);
        camera.setDisplayOrientation(90);

    }catch(Exception e){
        Log.d("exceptie", "nu a mers");
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
    Camera.Parameters params=camera.getParameters();
    params.setPreviewSize(width, height);
    camera.setParameters(params);
    camera.startPreview();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera.stopPreview();
    camera=null;
}

} `

Upvotes: 2

Views: 2807

Answers (2)

Gilad Haimov
Gilad Haimov

Reputation: 5857

You cannot make the camera preview transparent.

But you can make it hidden. Just set the preview surface size to 1x1 pixels:

   Camera.Parameters params = camera.getParameters();
   surfaceView.getLayoutParams().width = 1;
   surfaceView.getLayoutParams().height = 1;

This, BTW, is the trick behind one of the latest Android Security holes.

Gilad Haimov

www.mobileedge.co.il

Upvotes: 3

Ahmad Raza
Ahmad Raza

Reputation: 2850

Transparent color code is #00000000.. You are setting #800000 which isn't a transparent color.

Try setting the surfaceView color #00000000.

Hope this would help.

Upvotes: 0

Related Questions