因公司项目需要,要做自定义相机,之前一直没接触过这方面的内容,然后在网上找demo 找到了两个demo 一个是仿微信多选,一个是仿58拍照,这两个在网上都可以搜到demo,然后我把两个demo合并起来,变成了我现在手机一次拍多张 选择多张的功能
下面是仿58拍照核心代码
在TakePhotoActivity.class中
package com.spring.myphoto;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class TakePhotoActivity extends Activity {
private CameraView cv = null;// 继承surfaceView的自定义view 用于存放照相的图片
private Camera camera;
private Button cameraPhoto;
private Button cameraOk;
private LinearLayout linearLayout;
private LinearLayout linearLayoutCamera;
private String cameraPath;
private ArrayList<String> listPath = new ArrayList<String>();//存放图片地址的集合
int i = 0; // 删除的图片的tag 从0开始
private Button cancle;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_photo);
linearLayout = (LinearLayout)findViewById(R.id.linearlayout_images);
linearLayoutCamera = (LinearLayout)findViewById(R.id.preciew);
cameraPhoto = (Button)findViewById(R.id.camera_photo);
cameraOk = (Button)findViewById(R.id.camera_ok);
cancle = (Button) findViewById(R.id.cancle);
cameraOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//点击拍照
Intent intent = new Intent(TakePhotoActivity.this,MainActivity.class);
Intent intent = getIntent();
intent.putStringArrayListExtra("listPath", listPath);
setResult(3, intent);
finish();
}
});
cameraPhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
camera.takePicture(null, null, picture);
}
});
// cancle.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// finish();
// }
// });
//初始化camera数据
openCamera();
}
private void getImageView(String path) {
int j = i ++;
final View view = getLayoutInflater().inflate(R.layout.camera_item, null);
final ImageView imageView = (ImageView)view.findViewById(R.id.photoshare_item_image);
final Button button = (Button)view.findViewById(R.id.photoshare_item_delete);
LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width = 250;
layoutParams.height = 250;
imageView.setLayoutParams(layoutParams);
try {
imageView.setImageBitmap(getImageBitmap(path));
//做一个旋转,因为竖屏拍照时,下面显示的缩略图方向与预览时不一致
RotateAnimation animation =new RotateAnimation(0f,90f,Animation.RELATIVE_TO_SELF,
0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(0);
animation.setFillAfter(true);
imageView.startAnimation(animation);
button.setTag(j);// 给删除button设置tag
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
linearLayout.removeView(view);
int k = Integer.parseInt(button.getTag().toString());
listPath.set(k, "NOIMAGE");//如果点击删除,将存放path的集合中的值改为"NOIMAGE"
}
});
linearLayout.addView(view);
listPath.add(path);
}
// 根据路径获取图片
private Bitmap getImageBitmap(String path) throws FileNotFoundException, IOException{
Bitmap bmp = null;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opts);
opts.inSampleSize = ImageTools.computeSampleSize(opts, -1, 150*150);//
opts.inJustDecodeBounds = false;
try {
bmp = BitmapFactory.decodeFile(path, opts);
} catch (OutOfMemoryError e) {
}
return bmp;
}
// 主要的surfaceView,负责展示预览图片,camera的开关
class CameraView extends SurfaceView {
private SurfaceHolder holder = null;
public CameraView(Context context) {
super(context);
holder = this.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = camera.getParameters();
camera.setParameters(parameters);
camera.startPreview();
}
@SuppressLint("NewApi")
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
//设置预览角度
camera.setDisplayOrientation(90);
// 设置holder主要是用于surfaceView的图片的实时预览,以及获取图片等功能
camera.setPreviewDisplay(holder);
} catch (IOException e) {
camera.release();
camera = null;
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
});
}
}
private void openCamera() {
if (cv == null) {
linearLayoutCamera.removeAllViews();
cv = new CameraView(TakePhotoActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
linearLayoutCamera.addView(cv, params);
} else {
try {
if (camera != null) {
camera.startPreview();
}
} catch (Exception e) {
LogUtil.i("",
"Error starting camera preview: " + e.getMessage());
}
}
}
// 回调用的picture,实现里边的onPictureTaken方法,其中byte[]数组即为照相后获取到的图片信息
private Camera.PictureCallback picture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File picture = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".jpg");
try {
cameraPath = picture.getPath();
FileOutputStream fos = new FileOutputStream(cameraPath);// 获得文件输出流
fos.write(data);// 写入文件
fos.close();// 关闭文件流
openCamera();// 重新打开相机
getImageView(cameraPath);// 获取照片
} catch (Exception e) {
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
if(requestCode == 1){
getImageView(ImageTools.getAbsoluteImagePath(data.getData(),TakePhotoActivity.this));
}
}
}
}
ImageTools.java
package com.spring.myphoto;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
public final class ImageTools {
/**
*
* */
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;
}
private 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) ? 150 : (int) Math.min(Math.floor(w / minSideLength),Math.floor(h / minSideLength));
if (upperBound < lowerBound){
return lowerBound;
}
if((maxNumOfPixels == -1) && (minSideLength == -1)){
return 1;
}else if (minSideLength == -1){
return lowerBound;
}else {
return upperBound;
}
}
/**
* 获取图片绝对路径
*
* */
public static String getAbsoluteImagePath(Uri uri, Activity activity){
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = activity.managedQuery(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
MainActivity.java
package com.spring.myphoto;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private ImageView ivTakePhoto;
private PopupWindow pop = null;
private LinearLayout ll_popup;
private View parentView;
public static Bitmap bimap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parentView = getLayoutInflater().inflate(R.layout.activity_main,
null);
setContentView(parentView);
setContentView(R.layout.activity_main);
ivTakePhoto = (ImageView) findViewById(R.id.iv_add);
ivTakePhoto.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_add:
ll_popup.startAnimation(AnimationUtils.loadAnimation(
MainActivity.this, R.anim.activity_translate_in));
pop.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
break;
default:
break;
}
}
public void Init() {
pop = new PopupWindow(MainActivity.this);
View view = getLayoutInflater().inflate(R.layout.item_popupwindows,
null);
ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
pop.setWidth(LayoutParams.MATCH_PARENT);
pop.setHeight(LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setFocusable(true);
pop.setOutsideTouchable(true);
pop.setContentView(view);
RelativeLayout parent = (RelativeLayout) view.findViewById(R.id.parent);
Button bt1 = (Button) view.findViewById(R.id.item_popupwindows_camera);
Button bt2 = (Button) view.findViewById(R.id.item_popupwindows_Photo);
Button bt3 = (Button) view.findViewById(R.id.item_popupwindows_cancel);
parent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pop.dismiss();
ll_popup.clearAnimation();
}
});
bt1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
photo();
pop.dismiss();
ll_popup.clearAnimation();
}
private void photo() {
Intent intent = new Intent(MainActivity.this,TakePhotoActivity.class);
startActivity(intent);
}
});
bt2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//进入选择图库
pop.dismiss();
ll_popup.clearAnimation();
}
});
bt3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pop.dismiss();
ll_popup.clearAnimation();
}
});
}
}
activity_take_photo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="@+id/preciew"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</LinearLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:background="#e7e7e7"
android:id="@+id/camera_menu"
>
<Button
android:layout_height="80dp"
android:layout_width="80dp"
android:layout_alignParentLeft="true"
android:id="@+id/cancle"
android:background="@null"
android:text="取消"
/>
<Button
android:layout_height="80dp"
android:layout_width="80dp"
android:background="@drawable/shot_h"
android:layout_centerHorizontal="true"
android:id="@+id/camera_photo"
/>
<Button
android:layout_height="80dp"
android:layout_width="80dp"
android:layout_alignParentRight="true"
android:background="@null"
android:id="@+id/camera_ok"
android:text="确定"
/>
</RelativeLayout>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/gallery_images"
android:layout_above="@id/camera_menu"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:padding="10dp"
android:id="@+id/linearlayout_images"
android:layout_marginTop="40dp"
>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
camera_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/photoshare_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:src="@drawable/ic_launcher" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/delete"
android:id="@+id/photoshare_item_delete"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
这些是在原demo中的基础上有所修改–仅仅是拍多张照片
以后会有选择多张照片的博客
还有关于拍照横竖屏显示问题还有自定义相机方面的问题可以相互交流
qq 2240405944