调用摄像头和相册,从相册中选择(含有google发布的图片压缩以及自己的图片截取)
来源:互联网 发布:java的方法的重载 编辑:程序博客网 时间:2024/06/04 17:57
主程序
package com.test.myphotoshop;import android.support.v7.app.ActionBarActivity;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.media.audiofx.EnvironmentalReverb;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity implements OnClickListener{ public static final int TAKE_PHOTO=1; public static final int CROP_PHOTO=2; private Button mButtonTakePhoto; private Button mChooseFromAlbum; private ImageView mPicture; private Uri imageUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonTakePhoto=(Button) findViewById(R.id.button_take_photo); mChooseFromAlbum=(Button) findViewById(R.id.button_choose_photo); mChooseFromAlbum.setOnClickListener(this); mPicture=(ImageView) findViewById(R.id.picture); mButtonTakePhoto.setOnClickListener(this); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case TAKE_PHOTO: if(resultCode==RESULT_OK){ Intent intent =new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri, "image/*"); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PHOTO); } break; case CROP_PHOTO: if(resultCode==RESULT_OK){ try { Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); mPicture.setImageBitmap(bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } default: break; } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_take_photo: File outputImage=new File(Environment.getExternalStorageDirectory(),"tempImage.jpg"); try { if(outputImage.exists()){ outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } imageUri=Uri.fromFile(outputImage); Intent intent =new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); //启动相机程序 startActivityForResult(intent, TAKE_PHOTO); break; case R.id.button_choose_photo: File outputImage2 =new File(Environment.getExternalStorageDirectory(),"output_image.jpg"); try { if(outputImage2.exists()){ outputImage2.delete(); } outputImage2.createNewFile(); } catch (IOException e) { e.printStackTrace(); } imageUri=Uri.fromFile(outputImage2); Intent intent2=new Intent("android.intent.action.GET_CONTENT"); intent2.setType("image/*"); intent2.putExtra("crop", true); intent2.putExtra("scale", true); intent2.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent2, CROP_PHOTO); break; default: break; } }}
布局文件
<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:orientation="vertical"> <Button android:id="@+id/button_take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Take Photo" /> <Button android:id="@+id/button_choose_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Choose From Album" /> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/></LinearLayout>
权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
图片压缩代码
主要是第一个方法,其他两个方法不用管,下面两个图片压缩方法是google提供的,想要压缩图片是只要把图片的路径传入即可。
//这个方法是自己写的,直接传入想压缩的图片的路径,然后把图片压缩后,直接保存。private void zipImage(String savePath) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(savePath, options); //设置图片的格式 options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(savePath, options); try { FileOutputStream fos = new FileOutputStream(savePath); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } bitmap.recycle(); bitmap = null; System.gc(); } public int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels); int roundedSize; if (initialSize <= 8) { roundedSize = 1; while (roundedSize < initialSize) { roundedSize <<= 1; } } else { roundedSize = (initialSize + 7) / 8 * 8; } return roundedSize; } private int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math .sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 128 : (int) Math.min( Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } }
最终版本
主程序
package com.test.myphotoshop;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import android.app.Activity;import android.content.Intent;import android.database.Cursor;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.media.audiofx.EnvironmentalReverb;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity implements OnClickListener { public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 2; public static final int TEACHER_PHOTO = 3; public static final int CHOOSE_PHOTO=4; private Button mButtonTakePhoto; private Button mChooseFromAlbum; private Button mTeacherPhoto; private ImageView mPicture; private File newfile; private Uri imageUri; private Uri dataPhoto; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonTakePhoto = (Button) findViewById(R.id.button_take_photo); mChooseFromAlbum = (Button) findViewById(R.id.button_choose_photo); mChooseFromAlbum.setOnClickListener(this); mPicture = (ImageView) findViewById(R.id.picture); mButtonTakePhoto.setOnClickListener(this); mTeacherPhoto = (Button) findViewById(R.id.button_Teacher_photo); mTeacherPhoto.setOnClickListener(this); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case TAKE_PHOTO: if (resultCode == RESULT_OK) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri, "image/*"); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PHOTO); } break; case CROP_PHOTO: if (resultCode == RESULT_OK) { try { Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); mPicture.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } case TEACHER_PHOTO: if (resultCode == 8) { ImageZip.zipImage(newfile.getAbsolutePath()); mPicture.setImageURI(Uri.fromFile(newfile)); } break; case CHOOSE_PHOTO: if (resultCode == RESULT_OK) { dataPhoto=data.getData(); String[] proj={MediaStore.Images.Media.DATA}; Cursor actualimagecursor=managedQuery(dataPhoto, proj, null, null, null); int actual_image_column_index=actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst(); String img_path=actualimagecursor.getString(actual_image_column_index); File file =new File(img_path); ImageZip.zipImage(file.getAbsolutePath()); mPicture.setImageURI(Uri.fromFile(file)); } break; default: break; } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_take_photo: File outputImage = new File(Environment.getExternalStorageDirectory(), "tempImage.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } imageUri = Uri.fromFile(outputImage); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); // 启动相机程序 startActivityForResult(intent, TAKE_PHOTO); break; case R.id.button_choose_photo: Intent intent2 = new Intent("android.intent.action.GET_CONTENT"); intent2.setType("image/*"); startActivityForResult(intent2, CHOOSE_PHOTO); break; case R.id.button_Teacher_photo: Intent intent3 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); newfile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg"); if (!newfile.exists()) { try { newfile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } setResult(8); intent3.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(newfile)); startActivityForResult(intent3, TEACHER_PHOTO); break; default: break; } }}
压缩图片的程序
package com.test.myphotoshop;import java.io.FileOutputStream;import java.io.IOException;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class ImageZip { public static void zipImage(String savePath) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(savePath, options); options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(savePath, options); try { FileOutputStream fos = new FileOutputStream(savePath); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } bitmap.recycle(); bitmap = null; System.gc(); } public static int computeSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels); int roundedSize; if (initialSize <= 8) { roundedSize = 1; while (roundedSize < initialSize) { roundedSize <<= 1; } } else { roundedSize = (initialSize + 7) / 8 * 8; } return roundedSize; } public static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math .sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 128 : (int) Math.min( Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } }}
布局文件
<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:orientation="vertical" > <Button android:id="@+id/button_take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Take Photo" /> <Button android:id="@+id/button_choose_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Choose From Album" /> <Button android:id="@+id/button_Teacher_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Teacher Photo" /> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /></LinearLayout>
0 0
- 调用摄像头和相册,从相册中选择(含有google发布的图片压缩以及自己的图片截取)
- 调用摄像头和相册(调用摄像头拍照和从相册中选择图片)
- Android从相册选择图片和调用系统摄像头拍照
- 调用系统摄像头和相册选择图片
- Android多媒体之调用摄像头和从本地相册中选择图片
- android 相册选择图片 图片的压缩
- 从相机拍摄和从相册读取图片,以及对图片的压缩的完全解决方案
- 从相册和相机选择图片以及图片压缩和android7.0相机
- Android 调用摄像头拍照 以及 从相册中选择照片
- 从相册中选择图片
- android中调用相机、相册 以及图片压缩,文件压缩
- 相机的调用以及从相册中选取图片
- Camora调用系统的照相机和相册以及图片的压缩
- iOS从相册选择图片进行压缩
- 调用摄像头拍照和从相册中选择照片
- 调用摄像头拍照和从相册中选择
- 相册截取图片的URL
- 从相册选择图片
- 《机器学习》__note1
- 【Leet Code】 75.Sort Colors--medium
- eclipse 安装插件link方法
- Doxygen向导使用
- mysql group by 无数据补0 方法
- 调用摄像头和相册,从相册中选择(含有google发布的图片压缩以及自己的图片截取)
- hibernate核心开发接口
- cmd添加管理员
- tewsf
- 【Android技术整理】AsyncTask实现多线程处理
- C++的深拷贝与浅拷贝
- 【网络】(十)select的限制与解决方法poll
- 返回来源页
- NHibernate从入门到精通系列(1)——NHibernate概括