如何使相机预览透明?

时间:2014-06-25 12:08:35

标签: android camera surfaceview transparent preview

我已经看过很多关于相机预览的帖子,上面有一个透明的图像/按钮,但是我想知道如何制作半透明的相机预览,我试图在xml中使用#800000透明的Surfaceview文件,但它对相机预览没有任何影响..我试图制作类似于透明屏幕的应用程序。这是我提供的服务:

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;
}

} `

2 个答案:

答案 0 :(得分:3)

您无法使相机预览透明。

但你可以隐藏它。只需将预览曲面大小设置为1x1像素:

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

这是BTW,是最新Android Security holes之后的诀窍。

Gilad Haimov

www.mobileedge.co.il

答案 1 :(得分:0)

透明色码为#00000000 ..您设置的#800000不是透明色。

Try setting the surfaceView color #00000000.

希望这会有所帮助。