1、应用主要是调用了startActivityForResult方法来进行拍照,通过设置照片存储的路径,将拍好的照片存储在对应的路径下,并可以查看拍好的照片。
代码下载地址:http://download.csdn.net/detail/u011324501/9498516
2、主要代码:onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode== Activity.RESULT_OK){
String sdStatus = Environment.getExternalStorageState();//检测sd卡是否可用
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)){
System.out.println("SD现在不能使用!");
return ;
}
String name = new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
Bundle bundle = data.getExtras();
bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
getRoundedBitmap(bitmap);
FileOutputStream b = null;
File file = new File("/sdcard/data/");//文件保存路径
file.mkdirs();// 创建文件夹
String fileName ="/sdcard/data/"+name;//文件名字
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
} catch (Exception e) {
e.printStackTrace();
}finally {
try {//用完关闭
b.flush();
b.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (resultCode==Activity.RESULT_CANCELED){
Toast.makeText(this, "已取消!", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
执行拍照的代码:
takephoto = (Button)findViewById(R.id.photo);
takephoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
});
3、应用不仅执行了拍照存储查看,还添加了几种照片的处理
全部代码:MainActivity.java
package wind.com.camera;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button takephoto;
private Button hui,alpaha,rotate,screw,Reflected;
ImageView image;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initUi();
}
/**
* 初始化UI
*/
private void initUi() {
image = (ImageView)findViewById(R.id.image);// 将图片显示在ImageView里
//拍照
takephoto = (Button)findViewById(R.id.photo);
takephoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
});
hui = (Button)findViewById(R.id.hui);
hui.setOnClickListener(this);
alpaha = (Button)findViewById(R.id.alpaha);
alpaha.setOnClickListener(this);
rotate = (Button)findViewById(R.id.rotate);
rotate.setOnClickListener(this);
screw = (Button)findViewById(R.id.screw);
screw.setOnClickListener(this);
Reflected = (Button)findViewById(R.id.Reflected);
Reflected.setOnClickListener(this);
}
/**
*
* @param v
*/
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.hui: getGrayBitmap(bitmap);
Toast.makeText(this,"变灰",Toast.LENGTH_SHORT).show();
break;
case R.id.alpaha:getAlphaBitmap(bitmap);
Toast.makeText(this,"alpaha",Toast.LENGTH_SHORT).show();
break;
case R.id.rotate:getRotatedBitmap(bitmap);
Toast.makeText(this,"rotate",Toast.LENGTH_SHORT).show();
break;
case R.id.screw:getScrewBitmap(bitmap);
Toast.makeText(this,"screw",Toast.LENGTH_SHORT).show();
break;
case R.id.Reflected: getReflectedBitmap(bitmap);
Toast.makeText(this,"Reflected",Toast.LENGTH_SHORT).show();
break;
}
}
/**
* take photo
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode== Activity.RESULT_OK){
String sdStatus = Environment.getExternalStorageState();//检测sd卡是否可用
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)){
System.out.println("SD现在不能使用!");
return ;
}
String name = new DateFormat().format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA)) + ".jpg";
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
Bundle bundle = data.getExtras();
bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
getRoundedBitmap(bitmap);
FileOutputStream b = null;
File file = new File("/sdcard/data/");//文件保存路径
file.mkdirs();// 创建文件夹
String fileName ="/sdcard/data/"+name;//文件名字
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
} catch (Exception e) {
e.printStackTrace();
}finally {
try {//用完关闭
b.flush();
b.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (resultCode==Activity.RESULT_CANCELED){
Toast.makeText(this, "已取消!", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 图片圆角处理
* @param bitmap
* @return
*/
public Bitmap getRoundedBitmap(Bitmap bitmap) {
// Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.frame);
Bitmap mBitmap = bitmap;
//创建新的位图
Bitmap bgBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
//把创建的位图作为画板
Canvas mCanvas = new Canvas(bgBitmap);
Paint mPaint = new Paint();
Rect mRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
RectF mRectF = new RectF(mRect);
//设置圆角半径为20
float roundPx = 15;
mPaint.setAntiAlias(true);
//先绘制圆角矩形
mCanvas.drawRoundRect(mRectF, roundPx, roundPx, mPaint);
//设置图像的叠加模式
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//绘制图像
mCanvas.drawBitmap(mBitmap, mRect, mRect, mPaint);
image.setImageBitmap(bgBitmap);
return bgBitmap;
}
/**
* 图片灰化处理
* @param bitmap
* @return
*/
public Bitmap getGrayBitmap(Bitmap bitmap) {
// Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Bitmap mBitmap = bitmap;
Bitmap mGrayBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(mGrayBitmap);
Paint mPaint = new Paint();
//创建颜色变换矩阵
ColorMatrix mColorMatrix = new ColorMatrix();
//设置灰度影响范围
mColorMatrix.setSaturation(0);
//创建颜色过滤矩阵
ColorMatrixColorFilter mColorFilter = new ColorMatrixColorFilter(mColorMatrix);
//设置画笔的颜色过滤矩阵
mPaint.setColorFilter(mColorFilter);
//使用处理后的画笔绘制图像
mCanvas.drawBitmap(mBitmap, 0, 0, mPaint);
image.setImageBitmap(mGrayBitmap);
return mGrayBitmap;
}
/**
* 提取图像Alpha位图
* @param bitmap
* @return mAlphaBitmap
*/
public Bitmap getAlphaBitmap(Bitmap bitmap) {
BitmapDrawable mBitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.mipmap.ic_launcher);
//Bitmap mBitmap = mBitmapDrawable.getBitmap();
Bitmap mBitmap = bitmap;
//BitmapDrawable的getIntrinsicWidth()方法,Bitmap的getWidth()方法
//注意这两个方法的区别
//Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmapDrawable.getIntrinsicWidth(), mBitmapDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(mAlphaBitmap);
Paint mPaint = new Paint();
mPaint.setColor(Color.BLUE);
//从原位图中提取只包含alpha的位图
Bitmap alphaBitmap = mBitmap.extractAlpha();
//在画布上(mAlphaBitmap)绘制alpha位图
mCanvas.drawBitmap(alphaBitmap, 0, 0, mPaint);
image.setImageBitmap(mAlphaBitmap);
return mAlphaBitmap;
}
/**
* getRotatedBitmap
* @param bitmap
* @return mRotateBitmap
*/
public Bitmap getRotatedBitmap(Bitmap bitmap) {
BitmapDrawable mBitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.mipmap.ic_launcher);
//Bitmap mBitmap = mBitmapDrawable.getBitmap();
Bitmap mBitmap = bitmap;
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preRotate(45);
Bitmap mRotateBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix, true);
image.setImageBitmap(mRotateBitmap);
return mRotateBitmap;
}
/**
* getScrewBitmap
* @param bitmap
* @return mScrewBitmap
*/
public Bitmap getScrewBitmap(Bitmap bitmap) {
BitmapDrawable mBitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.mipmap.ic_launcher);
//Bitmap mBitmap = mBitmapDrawable.getBitmap();
Bitmap mBitmap = bitmap;
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preSkew(1.0f, 0.15f);
Bitmap mScrewBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix, true);
image.setImageBitmap(mScrewBitmap);
return mScrewBitmap;
}
/**
* getReflectedBitmap
* @param bitmap
* @return mReflectedBitmap
*/
private Bitmap getReflectedBitmap(Bitmap bitmap) {
BitmapDrawable mBitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.mipmap.ic_launcher);
// Bitmap mBitmap = mBitmapDrawable.getBitmap();
Bitmap mBitmap = bitmap;
int width = mBitmap.getWidth();
int height = mBitmap.getHeight();
Matrix matrix = new Matrix();
// 图片缩放,x轴变为原来的1倍,y轴为-1倍,实现图片的反转
matrix.preScale(1, -1);
//创建反转后的图片Bitmap对象,图片高是原图的一半。
//Bitmap mInverseBitmap = Bitmap.createBitmap(mBitmap, 0, height/2, width, height/2, matrix, false);
//创建标准的Bitmap对象,宽和原图一致,高是原图的1.5倍。
//注意两种createBitmap的不同
//Bitmap mReflectedBitmap = Bitmap.createBitmap(width, height*3/2, Config.ARGB_8888);
Bitmap mInverseBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix, false);
Bitmap mReflectedBitmap = Bitmap.createBitmap(width, height*2, Bitmap.Config.ARGB_8888);
// 把新建的位图作为画板
Canvas mCanvas = new Canvas(mReflectedBitmap);
//绘制图片
mCanvas.drawBitmap(mBitmap, 0, 0, null);
mCanvas.drawBitmap(mInverseBitmap, 0, height, null);
//添加倒影的渐变效果
Paint mPaint = new Paint();
Shader mShader = new LinearGradient(0, height, 0, mReflectedBitmap.getHeight(), 0x70ffffff, 0x00ffffff, Shader.TileMode.MIRROR);
mPaint.setShader(mShader);
//设置叠加模式
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
//绘制遮罩效果
mCanvas.drawRect(0, height, width, mReflectedBitmap.getHeight(), mPaint);
image.setImageBitmap(mReflectedBitmap);
return mReflectedBitmap;
}
}
布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="wind.com.camera.MainActivity">
<Button
android:id="@+id/photo"
android:layout_width="fill_parent"
android:text="拍照"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/hui"
android:layout_width="wrap_content"
android:text="灰度"
android:layout_height="wrap_content" />
<Button
android:id="@+id/alpaha"
android:layout_width="wrap_content"
android:text="缩放"
android:layout_height="wrap_content" />
<Button
android:id="@+id/rotate"
android:layout_width="wrap_content"
android:text="旋转"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/screw"
android:layout_width="wrap_content"
android:text="倾斜"
android:layout_height="wrap_content" />
<Button
android:id="@+id/Reflected"
android:layout_width="wrap_content"
android:text="倒影"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>