以使用的服务的照片OnePlus一个 - 使用窗口管理器破解管理器、窗口、照片、OnePlus

2023-09-05 02:59:35 作者:你会腻我何必

我想拍摄照片​​时不向用户显示任何东西(没有意见),通过服务。这个问题已经被问过好几次,我已经通过所有我能找到了。一些类似的问题:

I'm trying to take a picture without showing the user anything (no view) through a service. This question has been asked several times before and I've gone through all I could find. Some similar questions:

的Andr​​oid Camera.takePicture失败

Android相机无法拍照,从后台服务

多数问题链接到其它问题,而不提供一个新的解决方案。

Most of the questions link to other questions without providing a new solution.

我相信这是解决这个问题的最好办法: http://stackoverflow.com/a/10268650/3860594 不幸的是,人没有提供一个完整的答案,我无法再现他的方法。

I believe this is the best way to solve this problem: http://stackoverflow.com/a/10268650/3860594 Unfortunately the person has not provided a complete answer and I'm having trouble reproducing his method.

我想要做的就是创建一个SurfaceHolder内SurfaceView。然后,我会用窗口管理与SurfaceView创造各种各样是完全透明的,因此,它的用户隐藏浮动窗口。请纠正我,如果我错了。

What I'm trying to do is create a SurfaceView inside a SurfaceHolder. I will then use WindowManager with the SurfaceView to create a floating window of sorts that is completely transparent so that it's hidden from the user. Please correct me if I'm wrong.

下面是我的code:

SurfaceView mview = new SurfaceView(this);
SurfaceHolder mholder = new SurfaceHolder() {
    @Override
    public void addCallback(Callback callback) {

    }

    @Override
    public void removeCallback(Callback callback) {

    }

    @Override
    public boolean isCreating() {
        return false;
    }

    @Override
    public void setType(int type) {

    }

    @Override
        public void setFixedSize(int width, int height) {
    }

    @Override
    public void setSizeFromLayout() {
    }

    @Override
    public void setFormat(int format) {

    }

    @Override
    public void setKeepScreenOn(boolean screenOn) {
    }

    @Override
    public Canvas lockCanvas() {
        return null;
    }

    @Override
    public Canvas lockCanvas(Rect dirty) {
        return null;
    }

    @Override
        public void unlockCanvasAndPost(Canvas canvas) {

    }

    @Override
    public Rect getSurfaceFrame() {
        return null;
    }

    @Override
    public Surface getSurface() {
        return null;
    }
};


WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
        PixelFormat.TRANSLUCENT);
wm.addView(mview, params);
mview.setZOrderOnTop(true);
mholder.setFormat(PixelFormat.TRANSPARENT);


try {
    camera.setPreviewDisplay(mview.getHolder());
    camera.startPreview();
    camera.takePicture(null,null,photoCallback);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

这一切都不可以作为一般的消息的RuntimeException:takePicture失败显示。任何帮助将是真棒,谢谢。

None of this works as the usual message RuntimeException: takePicture failed is shown. Any help would be awesome, thanks.

推荐答案

它发生这样在拼图的最后一个砖只透露了一个星期的问题被问之后。问题是特定于特定的装置,即,一OnePlus

It happens so that the last brick in the puzzle was only revealed a week after the question was asked. The problem is specific to a particular device, namely, OnePlus One.

样品code在 https://github.com/alexcohn/CameraInService 展示了如何这可以处理;最新提交补充道之间的出发preVIEW和执行延时 takePicture()。在太太的OnePlus一个实验表明,100毫秒的延迟是正常的,但1毫秒的时间太短。

The sample code at https://github.com/alexcohn/CameraInService shows how this can be handled; the latest commit adds a delay between starting preview and performing takePicture(). The experiment on my wife's OnePlus One shows that delay of 100 ms is OK, but 1 ms is too short.