关闭

拍照选择-相册图片裁剪后保存在指定的地址

341人阅读 评论(0) 收藏 举报

1,功能介绍:拍照或者是选取相册照片截取,存入指定的目录

===============================选取照片的activity==================================

package com.example.testandroid;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;


import com.example.testandroid.constance.Constant;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;


/**
 * 说明:主要用于选择文件操作
 */


public class SelectPicActivity extends Activity implements OnClickListener {
private TextView takePhotoBtn, albumBtn, cancelBtn;
private Intent lastIntent;


private static final int REQUEST_PHOTOALBUM = 1;// 相册
private static final int REQUEST_PHOTOGRAPH = 2;// 照相
private static final int REQUEST_PHOTOCROP = 3;// 裁剪
private Uri cameraUri; // 照相后图片保存路径
private String address;
private File fileDir;;// 保存图片的根目录

private static final String IMAGE_FILE_LOCATION = "file:///sdcard//LegalDaily/imageTemp/";//temp file
private Uri cropImageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.select_bottom_dialog);
initView();
}


/**
* 初始化加载View
*/
private void initView() {
takePhotoBtn = (TextView) findViewById(R.id.takephoto_text);
takePhotoBtn.setOnClickListener(this);
albumBtn = (TextView) findViewById(R.id.album_text);
albumBtn.setOnClickListener(this);
cancelBtn = (TextView) findViewById(R.id.cancle_text);
cancelBtn.setOnClickListener(this);
lastIntent = getIntent();

fileDir = new File(Constant.imageDir);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.takephoto_text:
// 拍照
photoGraph();
break;
case R.id.album_text:// 相册
photoAlbum();
break;
case R.id.cancle_text:
finish();
break;
default:
break;
}
}


/**
* 相册
*/
private void photoAlbum() {
Intent openAlbumIntent = new Intent(Intent.ACTION_PICK,
Media.EXTERNAL_CONTENT_URI);
startActivityForResult(openAlbumIntent, REQUEST_PHOTOALBUM);
}


/**
* 照相
*/
private void photoGraph() {
try {
Intent openCameraIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
String sdcardState = Environment.getExternalStorageState();
File file = null;
if (Environment.MEDIA_MOUNTED.equals(sdcardState)) {
// 是否有xxx.jpeg文件
file = new File(Constant.imageDir + System.currentTimeMillis()
+ ".JPEG");
}
if (file != null) {
cameraUri = Uri.fromFile(file);
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
startActivityForResult(openCameraIntent, REQUEST_PHOTOGRAPH);
}


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


private void startPhotoZoom(Uri uri) {
try {
// 获取系统时间 然后将裁剪后的图片保存至指定的文件夹
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
address = sDateFormat.format(new java.util.Date());
cropImageUri = Uri.parse(IMAGE_FILE_LOCATION + address+ ".JPEG");
Intent intent = new Intent("com.android.camera.action.CROP");
// 照片URL地址
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 500);
intent.putExtra("outputY", 500);
intent.putExtra("scale", true);
// 输出路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropImageUri);
// 输出格式
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
// 不启用人脸识别
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", false);
startActivityForResult(intent, REQUEST_PHOTOCROP);
} catch (Exception e) {
e.printStackTrace();
}

}


/**
* 保存裁剪之后的图片数据

* @param picdata
*/
private void setPicToView(Intent data) {
FileInputStream fis;
try {
fis = new FileInputStream(Constant.imageDir + address + ".JPEG");
Bitmap bitmap = BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bitmapByte = baos.toByteArray();
lastIntent.putExtra("bitmap", bitmapByte);
Constant.imagePath = new File(Constant.imageDir + address + ".JPEG");
setResult(Activity.RESULT_OK, lastIntent);
finish();
} catch (FileNotFoundException e) {
e.printStackTrace();

}

/**

* @param x
*            图像的宽度
* @param y
*            图像的高度
* @param image
*            源图片
* @param outerRadiusRat
*            圆角的大小
* @return 圆角图片
*/
public static Bitmap createFramedPhoto(int x, int y, Bitmap image, float outerRadiusRat) {
// 根据源文件新建一个darwable对象
Drawable imageDrawable = new BitmapDrawable(image);


// 新建一个新的输出图片
Bitmap output = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);


// 新建一个矩形
RectF outerRect = new RectF(0, 0, x, y);


// 产生一个红色的圆角矩形
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
canvas.drawRoundRect(outerRect, outerRadiusRat, outerRadiusRat, paint);


// 将源图片绘制到这个圆角矩形上
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
imageDrawable.setBounds(0, 0, x, y);
canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
imageDrawable.draw(canvas);
canvas.restore();


return output;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 0)
return;
switch (requestCode) {
case REQUEST_PHOTOGRAPH:// 拍照返回
startPhotoZoom(cameraUri);
break;
case REQUEST_PHOTOALBUM:// 相册返回
if (data != null) {
Uri uri = data.getData();
if (uri != null)
startPhotoZoom(uri);
}
break;
case REQUEST_PHOTOCROP:// 裁剪返回
if (data != null)
setPicToView(data);
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

}

===================================上面代码的一些地址参数Constance========================================

package com.example.testandroid.constance;
import java.io.File;
import android.os.Environment;
public class Constant {
public static String imageDir = Environment.getExternalStorageDirectory()
.getPath() + "/LegalDaily/imageTemp/";
public static File rootFile = new File(
Environment.getExternalStorageDirectory() + "/LegalDaily");


public static File imagePath = new File(rootFile, "crop.jpg");

}


0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:77745次
    • 积分:1950
    • 等级:
    • 排名:千里之外
    • 原创:49篇
    • 转载:110篇
    • 译文:56篇
    • 评论:8条
    最新评论