簡體   English   中英

如何在Camera Preview上繪制

[英]How to draw on Camera Preview

我從事此應用程序已有一段時間了,該應用程序允許用戶拍照。 有趣的是,應該在攝像機預覽上畫一些線。 問題是我不太確定如何執行此操作。 我確實有啟動照相機和畫線的代碼,但是我不知道如何合並它們。

我使用以下代碼在單擊按鈕時啟動攝像頭:

  initCamera.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            //Starting a new Intent
            /*Intent nextScreen = new Intent(getActivity().getApplicationContext(), CameraActivity.class);
            startActivity(nextScreen);*/
             count++;
            String file = dir+count+".jpg";
            File newfile = new File(file);
            try {
                newfile.createNewFile();
            } catch (IOException e) {}       

            Uri outputFileUri = Uri.fromFile(newfile);

            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);



        }
    });

這是我通常畫線的方式:

 DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    height = displaymetrics.heightPixels;
    width = displaymetrics.widthPixels;

    imageView1 = (ImageView) findViewById(R.id.imageView1);



    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    bmp = Bitmap.createBitmap(width, height, conf); 
    canvas = new Canvas(bmp);
    p = new Paint();
    p.setColor(Color.RED);
    imageView1.setImageBitmap(bmp);
    p.setStrokeWidth(5);
    canvas.drawLine(0, height/2, width, height/2, p);

您將需要一個疊加層才能在實時攝像機預覽的頂部繪制。 疊加層是在其上繪制預覽的曲面的子類。 預覽類如下所示:

class Preview extends ViewGroup implements SurfaceHolder.Callback,    Camera.PreviewCallback 
    {

        Preview(Context context) 
        {
            super(context);
            SurfaceView mSurfaceView;
            SurfaceHolder mHolder;

            mSurfaceView = new SurfaceView(PreviewContext);
            addView(mSurfaceView);

            // Install a SurfaceHolder.Callback so we get notified when the
            // underlying surface is created and destroyed.
            mHolder = mSurfaceView.getHolder();
            mHolder.addCallback(this);
            PreviewCameraOverlay = new CameraOverlay(context);
            addView(PreviewCameraOverlay);
        }
        // Additional functions like:
        // surfaceChanged(SurfaceHolder holder, int format, int width, int height)
        // onPreviewFrame(byte[] data, Camera pCamera)
        // etc.
    }

        protected class CameraOverlay extends View
        {
            public CameraOverlay(Context context)
            {
                super(context);

                setWillNotDraw(false);
                setBackgroundColor(Color.TRANSPARENT);
                setAlpha(1f);
                OverlayPaint = new Paint[PaintColors];
             }
          // Additional functions:
          // onDraw(Canvas canvas)
          // onMeasure(int widthMeasureSpec, int heightMeasureSpec)


         }

從您的主要活動中,您將調用: Preview CameraPreview = new Preview(this); 並從CameraOverlay的onDraw()繪制任何要繪制的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM