简体   繁体   English

在相机预览中没有使用setAlpha

[英]Not working setAlpha in camera preview

I want to create camera preview working on top of the Android system. 我想在Android系统上创建相机预览。 I also want to make it transparent in 50%. 我也想让它透明50%。 And here is the problem. 这就是问题所在。 setAlpha does not work for me when i use it. 使用它时, setAlpha对我不起作用。

This is my service, which i use to add SurfaceView to window: 这是我的服务,我用它将SurfaceView添加到窗口:

WindowManager.LayoutParams tmp = new WindowManager.LayoutParams(
            measuredWidth,measuredHeight,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                    | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            PixelFormat.TRANSPARENT);
    /*s.setZOrderOnTop(true);
    s.setAlpha(0.5f);
    tmp.alpha=0.0f;

    wm.addView(s, tmp);*/

    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View mCameraView = layoutInflater.inflate(R.layout.camera_surface, null);
    StunGunPreview preview=(StunGunPreview) mCameraView.findViewById(R.id.textureView);
    wm.addView(mCameraView, tmp);



public class StunGunPreview extends SurfaceView implements
        SurfaceHolder.Callback {
    SurfaceHolder holder;
    static Camera mCamera;
    Context c;
    WindowManager wm;

    public StunGunPreview(Context context, AttributeSet attrs) {
        super(context,attrs);
        c = context;
        wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        holder = this.getHolder();
        holder.addCallback(this);
        holder.setFormat(PixelFormat.TRANSPARENT);
       setZOrderOnTop(true);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        start_camera();
    }

    private void start_camera() {
        try {
            mCamera = Camera.open();
        } catch (RuntimeException e) {
            return;
        }
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (Exception e) {
            return;
        }
    }
}

A SurfaceView has two parts, the Surface and the View. SurfaceView有两个部分,Surface和View。 Calling the View's setAlpha() method will only affect content drawn on the View, which doesn't help you, because the Camera frames are being sent to the Surface. 调用View的setAlpha()方法只会影响在View上绘制的内容,这对您没有帮助,因为Camera帧正在发送到Surface。

The Surface is an independent layer. Surface是一个独立的层。 The current API doesn't provide a way to set a "plane alpha", so you just get whatever the pixel values have. 当前的API不提供设置“平面alpha”的方法,因此您只需获得像素值所具有的任何值。 For Camera and video output, the YUV-to-RGB conversion always generates fully opaque pixels. 对于相机和视频输出,YUV到RGB转换始终生成完全不透明的像素。

To do what you want, you'd need to send the Camera preview to a SurfaceTexture, which converts the frame to an OpenGL ES "external" texture. 要做你想做的事,你需要将Camera预览发送到SurfaceTexture,它将帧转换为OpenGL ES“外部”纹理。 You can then render that onto a partially-transparent quad. 然后,您可以将其渲染到部分透明的四边形上。 It's a fair bit of work, but once you have it working you can do just about anything with the live camera image (like wrap it around a sphere or send it bouncing around the screen). 这是一项相当多的工作,但是一旦你开始工作,你就可以使用实时相机图像做任何事情(比如将它包裹在球体周围或者将它发送到屏幕上)。

A simple example of this can be found in the "texture from camera" Activity in Grafika . 一个简单的例子可以在Grafika的“相机纹理”活动中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM