• android调用系统的相机服务


    本功能实现的是调用相机拍照,并利用img标签显示:

    思路一:拍照时刻进行保存

    package com.bobo.mypic;
    
    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import com.bobo.util.BitmapUtil;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
        private Button btn_takePic;
        private Button btn_fromPic;
        private File mediaFile;
        private int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
        private ImageView img_view;
        private Button btn_jump;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn_takePic = (Button) this.findViewById(R.id.btn_takePic);
             btn_fromPic = (Button) this.findViewById(R.id.btn_viewPic);
            btn_takePic.setOnClickListener(new TakePicListener());
            img_view = (ImageView) this.findViewById(R.id.img_viewPic);
            btn_jump = (Button) this.findViewById(R.id.btn_jump);
            btn_jump.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, SecondActivity.class);
                    startActivity(intent);
                }
            });
        }
    
        class TakePicListener implements View.OnClickListener {
    
            @Override
            public void onClick(View arg0) {
                takePhoto();
            }
    
        }
    
        private void takePhoto() {
            if (!Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                return;
            }
            File file = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                            + File.separator + "shareImg");
            if (!file.exists()) {
                if (!file.mkdirs())
                    return;
            }
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                    .format(new Date());
            mediaFile = new File(file.getPath() + File.separator + "IMG_"
                    + timeStamp + ".jpg");
            Uri fileUri = Uri.fromFile(mediaFile);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == this.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
                long len = mediaFile.length();
                if (len == 0) {
                    mediaFile = null;
                    return;
                }
                // 从文件中加载位图
                System.out.println("文件存储路径:" + mediaFile.getPath());
                Bitmap bitmap = new BitmapUtil().loadBitmap(mediaFile.getPath());
                img_view.setImageBitmap(bitmap);
            }
        }
    
    }


    其中,从文件中加载位图的代码如下:

    若是由于图片本身较大,直接用Widge中的图片控件打不开,但通过BitmapFactory.Options的inJustDecodeBounds和inSampleSize可以压缩打开,但是在通过手势放大的时候,图片本身就不是很清晰。

    package com.bobo.util;
    
    import java.io.FileOutputStream;
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.BitmapFactory.Options;
    
    public class BitmapUtil {
        // 从文件中加载位图
        public Bitmap loadBitmap(String path) {
            //这样可以显示,但是图片的质量很差
            BitmapFactory.Options opts = new Options();
            opts.inJustDecodeBounds = true;
            // 告诉解析�?不要真的去解析图�?只是把图片的宽高信息给提供出�?
            BitmapFactory.decodeFile(path, opts);
            int width = opts.outWidth;
            int height = opts.outHeight;
            // 得到手机屏幕的宽高信�?
            int windowwidth = 50;
            int windowheigth = 50;
    
            int scalex = width / windowwidth;
            int scaley = height / windowheigth;
    
            if (scalex > scaley && scaley > 1) { // 水平方向的缩放比例比较大
                opts.inSampleSize = scalex;
            }
            if (scaley > scalex && scalex > 1) { // 竖直方向的缩放比例比较大
                opts.inSampleSize = scaley;
            }
            opts.inJustDecodeBounds = false;
            // 告诉解析�?按照 opts.inSampleSize 比例真实的返回位�?
            Bitmap bitmap = BitmapFactory.decodeFile(path, opts);
    
            return bitmap;
        }
    
    }

    思路二,在activity的onactivityResult方法中进行保存

    package com.bobo.mypic;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Locale;
    
    import com.bobo.mypic.MainActivity.TakePicListener;
    import com.bobo.util.BitmapUtil;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.text.format.DateFormat;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class SecondActivity extends Activity {
        private Button btn_takePic;
        private Button btn_fromPic;
        private File mediaFile;
        private int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1;
        private ImageView img_view;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn_takePic = (Button) this.findViewById(R.id.btn_takePic);
            btn_fromPic = (Button) this.findViewById(R.id.btn_viewPic);
            btn_takePic.setOnClickListener(new TakePicListener());
            img_view = (ImageView) this.findViewById(R.id.img_viewPic);
        }
    
        class TakePicListener implements View.OnClickListener {
    
            @Override
            public void onClick(View arg0) {
                takePhoto();
            }
    
        }
    
        public void takePhoto() {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK) {
                String sdStatus = Environment.getExternalStorageState();
                if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
                    Log.i("TestFile",
                            "SD card is not avaiable/writeable right now.");
                    return;
                }
                String name = new DateFormat().format("yyyyMMdd_hhmmss",
                        Calendar.getInstance(Locale.CHINA))
                        + ".jpg";
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
    
                FileOutputStream b = null;
                File file = new File("/sdcard/myImage/");
                file.mkdirs();// 创建文件夹
                String fileName = "/sdcard/myImage/" + name;
    
                try {
                    b = new FileOutputStream(fileName);
                     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        b.flush();
                        b.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                ((ImageView) findViewById(R.id.img_viewPic)).setImageBitmap(bitmap);// 将图片显示在ImageView里
            }
        }
    
    }
  • 相关阅读:
    i5ting_doc的安装和使用
    vscode—修改默认的shell
    cookie的相关知识
    这是一段有毒的js代码,求大神解释!!!
    BFC的触发条件
    替换元素与非替换元素
    css中em的使用方法
    误操作导致ps界面中的工具栏消失
    导航栏里面的li标签和a标签的配合使用
    记录一下 elmentui 循环复选框不能选中问题
  • 原文地址:https://www.cnblogs.com/bobodeboke/p/3088797.html
Copyright © 2020-2023  润新知