package com.droidstouch.takephoto; |
003 | import java.io.ByteArrayOutputStream; |
006 | import android.app.Activity; |
007 | import android.content.Intent; |
008 | import android.graphics.Bitmap; |
009 | import android.net.Uri; |
010 | import android.os.Bundle; |
011 | import android.os.Environment; |
012 | import android.provider.MediaStore; |
013 | import android.view.View; |
014 | import android.view.View.OnClickListener; |
015 | import android.widget.Button; |
016 | import android.widget.ImageView; |
018 | public class PhotoActivity extends
Activity { |
019 | public
static final int NONE = 0 ; |
021 | public
static final int PHOTOHRAPH = 1 ; // 拍照 |
023 | public
static final int PHOTOZOOM = 2 ; // 缩放 |
025 | public
static final int PHOTORESOULT = 3 ; // 结果 |
027 | public
static final String IMAGE_UNSPECIFIED = "image/*" ; |
029 | ImageView imageView = null ; |
036 | public
void onCreate(Bundle savedInstanceState) { |
038 | super .onCreate(savedInstanceState); |
040 | setContentView(R.layout.photo); |
042 | imageView = (ImageView) findViewById(R.id.imageID); |
044 | button0 = (Button) findViewById(R.id.btn_01); |
046 | button1 = (Button) findViewById(R.id.btn_02); |
048 | button0.setOnClickListener( new
OnClickListener() { |
051 | public
void onClick(View v) { |
053 | Intent intent = new Intent(Intent.ACTION_PICK, null ); |
055 | intent.setDataAndType( |
056 | MediaStore.Images.Media.EXTERNAL_CONTENT_URI, |
059 | startActivityForResult(intent, PHOTOZOOM); |
065 | button1.setOnClickListener( new
OnClickListener() { |
068 | public
void onClick(View v) { |
070 | Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
072 | intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile( new
File( |
073 | Environment.getExternalStorageDirectory(), "temp.jpg" ))); |
075 | startActivityForResult(intent, PHOTOHRAPH); |
084 | protected
void onActivityResult( int
requestCode, int
resultCode, Intent data) { |
086 | if
(resultCode == NONE) |
092 | if
(requestCode == PHOTOHRAPH) { |
096 | File picture = new File(Environment.getExternalStorageDirectory() |
099 | startPhotoZoom(Uri.fromFile(picture)); |
109 | if
(requestCode == PHOTOZOOM) { |
111 | startPhotoZoom(data.getData()); |
117 | if
(requestCode == PHOTORESOULT) { |
119 | Bundle extras = data.getExtras(); |
123 | Bitmap photo = extras.getParcelable( "data" ); |
125 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
127 | photo.compress(Bitmap.CompressFormat.JPEG, 75 , stream); // (0 - |
130 | imageView.setImageBitmap(photo); |
136 | super .onActivityResult(requestCode,resultCode, data); |
140 | public
void startPhotoZoom(Uri uri) { |
142 | Intent intent = new Intent( "com.android.camera.action.CROP" ); |
144 | intent.setDataAndType(uri, IMAGE_UNSPECIFIED); |
146 | intent.putExtra( "crop" , "true" ); |
148 | // aspectX aspectY 是宽高的比例 |
150 | intent.putExtra( "aspectX" , 1 ); |
152 | intent.putExtra( "aspectY" , 1 ); |
154 | // outputX outputY 是裁剪图片宽�? |
156 | intent.putExtra( "outputX" , 64 ); |
158 | intent.putExtra( "outputY" , 64 ); |
160 | intent.putExtra( "return-data" , true ); |
162 | startActivityForResult(intent, PHOTORESOULT); |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理