安卓端调用相机拍照并返回预览图片
- 继之前写的安卓调用相机拍照返回预览,如果对于画质要求不高的话可以考虑之前的写的那篇,返回的是一个压缩后的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左右,而且每次拍照都有进行清除!所以也没啥影响!
- 写了一天代码了,眼睛有点疼!明天还要加班!就不多说了!如有错误还望指正!离职倒计时…