android - SurfaceView 拍照,怎么设置分辨率
怪我咯
怪我咯 2017-04-18 09:07:25
0
1
2682

1.华为荣耀4a测试,分辨率一直是160*120?
2.求助
代码如下:

 SurfaceHolder holder;
    Camera cam;
    Camera.Parameters myParameters;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo);
        initView();
    }

    private void initView() {
        //双缓冲view
        SurfaceView sfv= (SurfaceView) findViewById(R.id.surfaceView);

        ViewGroup.LayoutParams lp = sfv.getLayoutParams();
        lp.width = 750;
        lp.height =1200;
        sfv.setLayoutParams(lp);
        //帮助类对象
        holder = sfv.getHolder();
        //指定类型
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        //指定回调接口
        holder.addCallback(new A());

    }
    private class A implements SurfaceHolder.Callback {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {

            cam = Camera.open(0);
            try {
                myParameters = cam.getParameters();
                myParameters.setPreviewFpsRange(20, 40);
                myParameters.setPictureFormat(ImageFormat.JPEG);
                myParameters.set("jpeg-quality", 85);
             //   myParameters.setPictureSize(750, 1280);

                cam.setPreviewDisplay(holder);
                cam.setDisplayOrientation(90);
               // cam.setParameters(myParameters);
                cam.startPreview();


            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {

        }
    }
    public void take(View v){
        //创建图片
        cam.takePicture(null,null,new B());
    }

    private class B implements Camera.PictureCallback {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            try {
                Bitmap a= BitmapFactory.decodeByteArray(data,0,data.length);

                //当前时间的毫秒数
                long time = System.currentTimeMillis();
                String  path="/sdcard/"+time+".jpg";
                FileOutputStream out=new FileOutputStream(new File(path));
                //压缩
                a.compress(Bitmap.CompressFormat.JPEG,100,out);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
Ty80
Parameters params = mCamera.getParameters();
if (!Config.FOR_MTK) {
    params.setFocusMode(Parameters.FOCUS_MODE_AUTO);
} 
params.setPictureSize(1920, 1080);
mCamera.setParameters(params);
mCamera.takePicture(mShutterCallback, mRawPictureCallback,
mPostViewPictureCallback, mJpegPictureCallback);

我是这样写的,不过照片的最大分辨率是在Android底层编写的,所以这里写的分辨率都是在最大分辨率范围内修改的。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板