使用相机并包含布局图像

时间:2013-07-16 05:43:36

标签: android-layout android-camera

我有下一个代码,实际上我有一个显示相机预览的主图层,以及两个名为R.layout.overlay和R.layout.controls的布局,控件的布局只显示一个拍照的按钮,并且叠加层有一个图像,我尝试做的是,当我拍摄照片时,R.layout.overlay中的图像出现在照片的捕捉上。 在拍摄照片之前的预览时刻,它显示控制图像正常。 我不知道如何做到这一点,因为当我拍摄它所拍摄的照片但没有R.layout.overlay上的图像。 或者有没有办法用一些代码截取屏幕截图?这是我一直在考虑的其他选择,但问题是照片的大小与屏幕分辨率相同。

这是我的代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        drawingView = new DrawingView(this);
        LayoutParams layoutParamsDrawing 
            = new LayoutParams(LayoutParams.FILL_PARENT, 
                    LayoutParams.FILL_PARENT);
        this.addContentView(drawingView, layoutParamsDrawing);

        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.control, null);
        LayoutParams layoutParamsControl = new LayoutParams(LayoutParams.FILL_PARENT, 
                    LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);

        inflater = LayoutInflater.from(getBaseContext());
         View view = inflater.inflate(R.layout.overlay, null);
         LayoutParams layoutParamsControl2= new LayoutParams(LayoutParams.FILL_PARENT, 
                 LayoutParams.FILL_PARENT);
         this.addContentView(view, layoutParamsControl2);

        buttonTakePicture = (Button)findViewById(R.id.takepicture);
        buttonTakePicture.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                camera.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);
            }});

        LinearLayout layoutBackground = (LinearLayout)findViewById(R.id.background);
        layoutBackground.setOnClickListener(new LinearLayout.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                buttonTakePicture.setEnabled(false);
                camera.autoFocus(myAutoFocusCallback);
            }});

        prompt = (TextView)findViewById(R.id.prompt);
    }


    //Termina onCreate

    FaceDetectionListener faceDetectionListener
    = new FaceDetectionListener(){

        @Override
        public void onFaceDetection(Face[] faces, Camera camera) {

            if (faces.length == 0){
                prompt.setText(" No Face Detected! ");
                drawingView.setHaveFace(false);
            }else{
                prompt.setText(String.valueOf(faces.length) + " Face Detected :) ");
                drawingView.setHaveFace(true);
                detectedFaces = faces;
            }

            drawingView.invalidate();

        }};

    AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){

        @Override
        public void onAutoFocus(boolean arg0, Camera arg1) {
            // TODO Auto-generated method stub
            buttonTakePicture.setEnabled(true);
        }};

    ShutterCallback myShutterCallback = new ShutterCallback(){

        @Override
        public void onShutter() {
            // TODO Auto-generated method stub

        }};

    PictureCallback myPictureCallback_RAW = new PictureCallback(){

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            // TODO Auto-generated method stub

        }};

    PictureCallback myPictureCallback_JPG = new PictureCallback(){

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            // TODO Auto-generated method stub
            /*Bitmap bitmapPicture 
                = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);  */

            Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());

            OutputStream imageFileOS;
            try {
                imageFileOS = getContentResolver().openOutputStream(uriTarget);
                imageFileOS.write(arg0);
                imageFileOS.flush();
                imageFileOS.close();

                prompt.setText("Image saved: " + uriTarget.toString());

                Toast.makeText(AndroidCamera.this, "Saved", Toast.LENGTH_LONG).show();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            camera.startPreview();
            camera.startFaceDetection();
        }};

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
        if(previewing){
            camera.stopFaceDetection();
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();

                prompt.setText(String.valueOf(
                        "Max Face: " + camera.getParameters().getMaxNumDetectedFaces()));
                camera.startFaceDetection();
                previewing = true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

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

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

    private class DrawingView extends View{

        boolean haveFace;
        Paint drawingPaint;

        public DrawingView(Context context) {
            super(context);
            haveFace = false;
            drawingPaint = new Paint();
            drawingPaint.setColor(Color.GREEN);
            drawingPaint.setStyle(Paint.Style.STROKE); 
            drawingPaint.setStrokeWidth(2);
        }

        public void setHaveFace(boolean h){
            haveFace = h;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            if(haveFace){

                // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
                 // UI coordinates range from (0, 0) to (width, height).

                 int vWidth = getWidth();
                 int vHeight = getHeight();

                for(int i=0; i<detectedFaces.length; i++){

                    int l = detectedFaces[i].rect.left;
                    int t = detectedFaces[i].rect.top;
                    int r = detectedFaces[i].rect.right;
                    int b = detectedFaces[i].rect.bottom;
                    int left    = (l+1000) * vWidth/2000;
                    int top     = (t+1000) * vHeight/2000;
                    int right   = (r+1000) * vWidth/2000;
                    int bottom  = (b+1000) * vHeight/2000;
                    canvas.drawRect(
                            left, top, right, bottom,  
                            drawingPaint);
                }
            }else{
                canvas.drawColor(Color.TRANSPARENT);
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

您可以通过实际按下组合按钮来截取手机的屏幕截图。因此,我想应该有一种方法可以通过编程方式执行此操作,可能是通过覆盖某处的回调。点击此处:How to programmatically take a screenshot in Android?

但请记住,当您截取屏幕截图时,图像将具有与手机显示屏相同的分辨率,而在旧设备上则可能非常低。使用相机拍摄的照片将具有更好的分辨率。您仍然可以通过简单地将两个图像合并在一起,在拍摄的照片上添加图像。

相关问题