添加缩放到相机android

时间:2017-08-16 10:28:36

标签: android camera

我是Android的新手,目前正在开发一款使用相机API的应用。我在应用程序中实现缩放功能时遇到问题。我正在使用相机套件API来制作应用程序,相机套件Api不支持缩放控件。请帮我提供所需的代码。一个星期以后,我无法弄明白。我需要实现缩放到相机预览。

这是我的代码。

public class MicroscopeAcitvity extends AppCompatActivity implements View.OnClickListener {

    CameraView cameraView;
    ImageButton cameraTakePic;
    ImageButton buttonFlash;
    Camera mCamera; //= Camera.open();
    Camera.Parameters params;
    TextView T1,T2;
    int counter = 0;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_microscope_acitvity);

        cameraView = (CameraView) findViewById(R.id.camera);

        cameraTakePic = (ImageButton) findViewById(R.id.button_take_picture);
        cameraTakePic.setOnClickListener(this);

        buttonFlash = (ImageButton) findViewById(R.id.button_Flash);
        buttonFlash.setOnClickListener(this);

        T1 = (TextView) findViewById(R.id.flash_text1);
        T1.setOnClickListener(this);
        //T1.setVisibility(View.GONE);

        T2 = (TextView) findViewById(R.id.flash_text2);
        T2.setOnClickListener(this);
        //T2.setVisibility(View.GONE);


        cameraView.setCameraListener(new CameraListener() {


            @Override
            public void onPictureTaken(byte[] picture) {
                super.onPictureTaken(picture);

                // Create a bitmap
                Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
            }
        });



        cameraView.setFocus(CameraKit.Constants.FOCUS_TAP);

    }



    @Override
    protected void onResume() {
        super.onResume();
        cameraView.start();
    }

    @Override
    protected void onPause() {
        cameraView.stop();
        super.onPause();
    }


    @Override
    public void onClick(View v) {
        int id;
        id = v.getId();

        if(id == R.id.button_take_picture){
            cameraView.captureImage();
        }

        if(id == R.id.button_Flash){
           if(counter % 2 != 0){
               T1.setVisibility(View.VISIBLE);
               T2.setVisibility(View.VISIBLE);
               counter = counter+1;
           }
           else{
               T1.setVisibility(View.GONE);
               T2.setVisibility(View.GONE);
               counter = counter+1;
           }

        }

        if(id == R.id.flash_text1){
            T1.setTextColor(Color.RED);
            T2.setTextColor(Color.DKGRAY);
            cameraView.setFlash(CameraKit.Constants.FLASH_ON);
        }

        if (id == R.id.flash_text2){
            T2.setTextColor(Color.RED);
            T1.setTextColor(Color.DKGRAY);
            cameraView.setFlash(CameraKit.Constants.FLASH_OFF);
        }


    }
} 

1 个答案:

答案 0 :(得分:0)

如果你想通过捏缩放,你必须实现ScaleGestureDetector.SimpleOnScaleGestureListener

要设置缩放,只需更改相机参数

即可
Camera.Parameters parameters = camera.getParameters();
    parameters.setZoom(zoomFactorValue);
    camera.setParameters(parameters);

如果你有时间,你可以自己编写其他代码或查看他是如何做到的

http://opencamera.sourceforge.net/

他在表面视图上使用捏缩放(但他的代码效率不高,非常难看,在某些方面很难理解)