安卓端调用相机拍照返回并预览---清晰原图

  • 继之前写的安卓调用相机拍照返回预览,如果对于画质要求不高的话可以考虑之前的写的那篇,返回的是一个压缩后的bitmap ,预览后是个略缩图。之前我也用的是这个,但是博主需要用来提取特征用于算法比对。所以太糊了,只能另辟蹊径!所以又有了这篇博客!感兴趣的也可以去看看之前那篇博客!
  • 》》》安卓调用系统相机拍照并返回预览(略缩图)《《《
  • 此方法在于将拍好的照片存在本地内存中,然后返回Uri显示。

一、demo预览

先简单写一个demo,效果如图,点击拍照以后返回图片覆盖在指定位置。


在这里插入图片描述


点击拍照以后直接调用相机


在这里插入图片描述


拍照成功后返回显示预览


在这里插入图片描述


以上便是demo预览。


二、xml代码和activity简单代码描述。

布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10sp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/photoImageView"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:layout_marginTop="10sp"
            android:layout_marginBottom="20sp"
            android:layout_weight="1"
            android:src="@drawable/boundary" />

        <Button
            android:id="@+id/takePhotoButton"
            android:layout_width="200sp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/button_shape"
            android:onClick="onTakePhotoButtonClicked"
            android:text="拍    照"
            android:textSize="23sp" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

Activity代码:

	private Uri photoUri;
	//初始化activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.take_photo);
        photoImageView = (ImageView)findViewById(R.id.photoImageView);
	 }
 	//监听拍照按钮,点击拍照调用相机
    public void takePhoto() {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            File file = new File(Constants.ClientVerify.PIC_PATH);
            if (!file.exists()) {
                file.mkdir();
            }else{
            	//拍一次删一次,避免堆积
                file.delete();
                file.mkdir();
            }
            String fileName = CommonCode.getPhotoFileName() + ".jpg";
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            photoUri = Uri.fromFile(new File(Constants.ClientVerify.PIC_PATH + fileName));
            //文件的路径,用于后面返回获取byte流
            tempPath = Constants.ClientVerify.PIC_PATH + fileName;
            intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
            startActivityForResult(intent, Constants.ClientVerify.REQUEST_CODE);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == Constants.ClientVerify.REQUEST_CODE) {
            Log.d(TAG, "开始回调");
            Uri uri = null;
            if (data != null && data.getData() != null) {
                uri = data.getData();
            }
            if (uri == null) {
                if (photoUri != null) {
                    uri = photoUri;
                }
            }
            //根据Uri显示本地图片
            photoImageView.setImageURI(uri);
            Log.d("success","路径:" + uri.toString());
            photoBytes = CommonCode.getPhotoByUrl(tempPath);
        }
    }

相关调用方法和参数:

String PIC_PATH = Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_DCIM + File.separator;

Integer REQUEST_CODE = 2;

public static String getPhotoFileName() {
    Date date = new Date(System.currentTimeMillis());
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
    return "IMG_" + dateFormat.format(date);
}

以上便大功告成

三、老规矩再来写几个转换工具方法

根据文件路径获取byte流:

 public static byte[] getPhotoByUrl(String photoUrl) {
  	File file = new File(photoUrl);
    if (!file.exists()) {
        return null;
    }
    try {
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] temp = new byte[1024];
        int size = 0;
        while ((size = fis.read(temp)) != -1) {
            out.write(temp, 0, size);
        }
        fis.close();
        return out.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

获取当前设备的IP:

 public static String getIpAddressString() {
    try {
        for (Enumeration<NetworkInterface> enNetI = NetworkInterface
                .getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
            NetworkInterface netI = enNetI.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = netI
                    .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return "0.0.0.0";
}

:此方法会存在一个问题,画质过高对于设备可能造成OOM,所以可自行进行一定的压缩操作!博主调试用的设备画质较渣,拍出来的照片也就900kb左右,而且每次拍照都有进行清除!所以也没啥影响!


  • 写了一天代码了,眼睛有点疼!明天还要加班!就不多说了!如有错误还望指正!离职倒计时…
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

private_static

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值