From 3359b899614ce0163a0a44a26b10a88fac4e2688 Mon Sep 17 00:00:00 2001 From: Hackergeek <1224085894@qq.com> Date: Thu, 12 May 2016 13:46:02 +0800 Subject: [PATCH 1/2] lab08 #94 --- .../CustomCameraActivity.java | 172 ++++++++++++++++++ .../homeworks/net1314080903208/MActivity.java | 119 ++++++++++++ .../net1314080903208/ResultActivity.java | 41 +++++ .../res/layout/activity_custom_camera.xml | 33 ++++ app/src/main/res/layout/activity_m.xml | 30 +++ app/src/main/res/layout/activity_result.xml | 21 +++ 6 files changed, 416 insertions(+) create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/CustomCameraActivity.java create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/MActivity.java create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/ResultActivity.java create mode 100644 app/src/main/res/layout/activity_custom_camera.xml create mode 100644 app/src/main/res/layout/activity_m.xml create mode 100644 app/src/main/res/layout/activity_result.xml diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/CustomCameraActivity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/CustomCameraActivity.java new file mode 100644 index 00000000..e07f4d81 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/CustomCameraActivity.java @@ -0,0 +1,172 @@ +package skyward.com.camera; + +import android.app.Activity; +import android.content.Intent; +import android.graphics.ImageFormat; +import android.hardware.Camera; +import android.os.Bundle; +import android.util.Log; +import android.view.SurfaceHolder; +import android.view.SurfaceView; +import android.view.View; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * 自定义相机 + * 1.创建Camera对象 + * 2.创建SurfaceView对象 + * 3.关联Camera对象和SurfaceView对象 + * 4.调整相机的显示效果 + * 5.自定义相机预览界面 + * Created by skyward on 2016/4/29. + */ +public class CustomCameraActivity extends Activity implements SurfaceHolder.Callback{ + private static final String TAG = "CustomCameraActivity"; + private Camera mCamera; + private SurfaceView mPreview; + private SurfaceHolder mHolder; + private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() { + @Override + public void onPictureTaken(byte[] data, Camera camera) { + //生成照片 + File tempFile = new File("/sdcard/temp.png"); + try { + FileOutputStream fos = new FileOutputStream(tempFile); + fos.write(data); + fos.close(); + Intent intent = new Intent(CustomCameraActivity.this, ResultActivity.class); + Log.d(TAG, "onPictureTaken: " + tempFile.getAbsolutePath()); + intent.putExtra("picPath", tempFile.getAbsolutePath()); + startActivity(intent); + CustomCameraActivity.this.finish(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_custom_camera); + //2.创建SurfaceView对象 + mPreview = (SurfaceView) findViewById(R.id.preview); + // 点击屏幕时自动对焦 + mPreview.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + mCamera.autoFocus(null); + } + }); + //获取SurfaceHolder对象 + mHolder = mPreview.getHolder(); + mHolder.addCallback(this); + } + + /** + * 拍照 + * @param view + */ + public void capture(View view) { + //4.调整相机的显示效果 + //参数设置 + Camera.Parameters parameters = mCamera.getParameters(); + //设置照片格式 + parameters.setPictureFormat(ImageFormat.JPEG); + parameters.setPreviewSize(1280, 720); + //设置对焦模式为自动对焦 + parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); + mCamera.autoFocus(new Camera.AutoFocusCallback() { + @Override + public void onAutoFocus(boolean success, Camera camera) { + //当对焦成功,则拍照 + if (success) { + mCamera.takePicture(null, null, mPictureCallback); + } + } + }); + } + + @Override + protected void onResume() { + super.onResume(); + if(mCamera == null) { + // 1.创建Camera对象 + mCamera = getCamera(); + if (mHolder != null) { + setStartPreview(mCamera, mHolder); + } + } + } + + @Override + protected void onPause() { + super.onPause(); + releaseCamera(); + } + + /** + * 获取系统Camera对象 + * @return + */ + private Camera getCamera() { + Camera camera; + try { + camera = Camera.open(); + } catch (Exception e) { + camera = null; + e.printStackTrace(); + } + return camera; + } + + /** + * 开始预览相机内容 + */ + private void setStartPreview(Camera camera, SurfaceHolder holder) { + try { + //3.关联Camera对象和SurfaceView对象 + camera.setPreviewDisplay(holder); + //系统默认横屏显示 + //因此要将系统camera预览角度调整90度 + camera.setDisplayOrientation(90); + camera.startPreview(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * 释放相机资源 + */ + private void releaseCamera() { + if (mCamera != null) { + mCamera.setPreviewCallback(null); + mCamera.stopPreview(); + mCamera.release(); + mCamera = null; + } + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + setStartPreview(mCamera, mHolder); + } + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + mCamera.stopPreview(); + setStartPreview(mCamera, mHolder); + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + releaseCamera(); + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/MActivity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/MActivity.java new file mode 100644 index 00000000..9c7ad100 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/MActivity.java @@ -0,0 +1,119 @@ +package skyward.com.camera; + +import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.net.Uri; +import android.os.Environment; +import android.provider.MediaStore; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.ImageView; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +/** + * + */ +public class MActivity extends AppCompatActivity { + public static final int REQUEST_CODE_1 = 1; + public static final int REQUEST_CODE_2 = 2; + private static final String TAG = "MActivity"; + //文件路径 + private String mFilePath; + + private ImageView iv; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_m); + iv = (ImageView) findViewById(R.id.iv); + //获取SD卡路径(外部存储路径) /storage/emulated/0 + mFilePath = Environment.getExternalStorageDirectory().getPath(); + Log.d(TAG, "onCreate: " + mFilePath); + //获取公共外部存储空间路径 /storage/emulated/0/Pictures +// File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); +// Log.d(TAG, "onCreate: " + file.getAbsolutePath()); + mFilePath = mFilePath + "/temp.png"; + } + + /** + * 调用系统相机(返回缩略图) + * @param view + */ + public void startCamera1(View view) { + //通过隐式intent启动相机应用 + Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + startActivityForResult(intent, REQUEST_CODE_1); + } + + /** + * 调用系统相机(返回原图) + * @param view + */ + public void startCamera2(View view) { + //通过隐式intent启动相机应用 + Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + Uri photoUri = Uri.fromFile(new File(mFilePath)); + //修改系统保存图片的路径 + intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); + startActivityForResult(intent, REQUEST_CODE_2); + } + + /** + * 调用自定义相机 + * @param view + */ + public void customCamera(View view) { + Intent intent = new Intent(MActivity.this, CustomCameraActivity.class); + startActivity(intent); + } + + //将照片添加至系统相册 + private void galleryAddPic() { + Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); + File f = new File(mFilePath); + Uri contentUri = Uri.fromFile(f); + mediaScanIntent.setData(contentUri); + this.sendBroadcast(mediaScanIntent); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if(resultCode == RESULT_OK) { + if(requestCode == REQUEST_CODE_1) { + /** + * 系统相机默认返回的bitmap为缩略图 + */ + Bundle bundle = data.getExtras(); + Bitmap bitmap = (Bitmap) bundle.get("data"); + iv.setImageBitmap(bitmap); + } else if(requestCode == REQUEST_CODE_2) { + /** + * 要想使系统相机返回原图 + * 1.修改系统默认保存照片路径 + * 2.自定义照片文件保存路径,在调用系统相机之前,将路径作为参数传递给系统相机 + * 3.创建文件输入流,读取照片文件并转换为Bitmap,显示在ImageView中 + * + * 有可能会因为照片文件过大以致抛出内存溢出异常,不能够将照片显示在ImageView中 + */ + try { + FileInputStream fis = new FileInputStream(mFilePath); + Bitmap bitmap = BitmapFactory.decodeStream(fis); + Log.d(TAG, "onActivityResult: 读取图片"); + iv.setImageBitmap(bitmap); + galleryAddPic(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } finally { + + } + } + } + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/ResultActivity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/ResultActivity.java new file mode 100644 index 00000000..deba2341 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/net1314080903208/ResultActivity.java @@ -0,0 +1,41 @@ +package skyward.com.camera; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Matrix; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.widget.ImageView; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; + +/** + * 5.自定义相机预览界面 + * 相片预览界面 + */ +public class ResultActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_result); + //获取相片所在的路径 + String path = getIntent().getStringExtra("picPath"); + ImageView pic = (ImageView) findViewById(R.id.pic); + //系统默认拍摄的相片为横屏显示,因此要将图片旋转90度后再显示 + try { + FileInputStream fis = new FileInputStream(path); + Bitmap bitmap = BitmapFactory.decodeStream(fis); + Matrix matrix = new Matrix(); + //将图片旋转90度 + matrix.setRotate(90); + bitmap = Bitmap.createBitmap(bitmap, 0,0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); + pic.setImageBitmap(bitmap); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } +// Bitmap bitmap = BitmapFactory.decodeFile(path); +// pic.setImageBitmap(bitmap); + } +} diff --git a/app/src/main/res/layout/activity_custom_camera.xml b/app/src/main/res/layout/activity_custom_camera.xml new file mode 100644 index 00000000..245766e1 --- /dev/null +++ b/app/src/main/res/layout/activity_custom_camera.xml @@ -0,0 +1,33 @@ + + + +