zoukankan      html  css  js  c++  java
  • Android 打开照相机、获取相册图片、获取图片并裁减

    一.调用照相机

      注:surfaceView在当Activity不在前台的时候,会被销毁(onPause方法之后,执行销毁方法)当Activity回到前台时,在Activity执行onResume方法之后,会被重新创建,虽然如此,但并不需要在onResume方法中获取创建事件,因为surfaceView的生命周期可以通过其自身回调函数获取;

    public class MainActivity extends Activity {
        private Camera camera;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            SurfaceView sf = (SurfaceView) findViewById(R.id.sv);
            sf.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            sf.getHolder().setFixedSize(176, 176);
            sf.getHolder().setKeepScreenOn(true);
            sf.getHolder().addCallback(new Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    camera = Camera.open();
                    Camera.Parameters params = camera.getParameters();
                    Log.e("camera", params.flatten()); // 获取可以设置的参数
                    // params.setPreviewSize(480, 800);
                    params.setPreviewFrameRate(5); // 每秒帧数
                    params.setPictureSize(1024, 768); // 图片大小
                    params.setJpegQuality(80); // 图片质量
                    camera.setParameters(params);
                    try {
                        camera.setPreviewDisplay(holder);
                        camera.startPreview();
                    } catch (IOException e) {
                    }
                }
                @Override
                public void surfaceChanged(SurfaceHolder holder, int format,
                        int width, int height) {
                }
                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                    if (camera != null) {
                        camera.release(); // 不用之后要释放该对象
                    }
                }
            });
        }

       public void takePicture(View v) { if (camera != null) { switch (v.getId()) { case R.id.take_picture: camera.takePicture(null, null, new LSPictureCallback()); break; case R.id.auto_focus: camera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { } }); break; } } }
    private class LSPictureCallback implements PictureCallback { @Override public void onPictureTaken(byte[] data, Camera camera) { File jpgFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ""); FileOutputStream outStream = null; try { outStream = new FileOutputStream(jpgFile); outStream.write(data); outStream.close(); camera.startPreview(); // 照相保存图片之后继续进行画面预览 } catch (Exception e) { } } } }

    二.获取系统默认的相册中的图片的方法
      1、打开相册

                    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(i, 0x3001);

      2、获取选择结果,重写onActivityResult方法

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 0x3001 && resultCode == RESULT_OK && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };
    
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
    
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();
                iv.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            }
        }

    三、 获取图片并截取

                    File tempFile = new File(Environment.getExternalStorageDirectory().getPath() + "/cpm.jpg");
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("image/*");
                    intent.putExtra("crop", "true");// 截取图片
                    intent.putExtra("aspectX", 1);//
                    intent.putExtra("aspectY", 1);// 表示x/y方向上的比例,在此设置为1:2
                    intent.putExtra("output", Uri.fromFile(tempFile));
                    intent.putExtra("outputFormat", "JPEG");
                    startActivity(intent);
  • 相关阅读:
    未能加载文件或程序集''file:///D:/Program Files (x86)/ArcGIS/DeveloperKit10.0/DotNet/ESRI.ArcGIS.ADF.Local.dll'' 或它的某一个依赖项。试图加载格式不正确的程序。
    [GL]三维场景的组织
    一张图,把我震惊了【转】
    [WorldWind学习]8.Cache对象
    [WorldWind学习]6.World类
    七桥问题及一笔画
    VS下Qt4.8.4安装
    [WorldWind学习]5.相机对象
    [WorldWind学习]1.接触WorldWind项目
    [WorldWind学习]2.WorldWindow控件
  • 原文地址:https://www.cnblogs.com/a284628487/p/3194516.html
Copyright © 2011-2022 走看看