import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.os.Bundle; import android.os.Environment; import android.preference.DialogPreference; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import static android.graphics.PixelFormat.*; /** * Created by Administrator on 2016/1/21. */ public class CameraActivity extends Activity { private Camera camera; private boolean isPrivate = false;//是否为预览模式 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.camera); //判断是否安装SD卡 if(!android.os.Environment.getExternalStorageState().equals(Environment .MEDIA_MOUNTED)){ Toast.makeText(this,"未安装SD卡",Toast.LENGTH_LONG).show(); return; } SurfaceView sv = (SurfaceView) findViewById(R.id.sufaceview1); final SurfaceHolder sh = sv.getHolder(); Button yl = (Button) findViewById(R.id.yl); Button pz = (Button) findViewById(R.id.pz); sh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//设置该surface不维护缓冲 final Camera.PictureCallback jpeg = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { //根据拍照所得的数据创建位图 final Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length); //加载layout/save.xml文件对应的布局资源 View saveView = getLayoutInflater().inflate(R.layout.save,null); final EditText photoName = (EditText) saveView.findViewById(R.id.photo_name); ImageView show = (ImageView) saveView.findViewById(R.id.show); show.setImageBitmap(bitmap); camera.stopPreview(); isPrivate = false; new AlertDialog.Builder(CameraActivity.this).setView(saveView) .setPositiveButton("保存", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { File file = new File("sdcard/pictures/" + photoName .getText().toString().trim() + "jpg"); try { file.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); isPrivate = true; resetCamera(); } catch (IOException e) { e.printStackTrace(); } } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { isPrivate = true; resetCamera(); } }).show(); } }; yl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!isPrivate) {//如果相机为非预览模式,则打开相机 camera = Camera.open(); } try { camera.setPreviewDisplay(sh); Camera.Parameters parameters = camera.getParameters();//获取相机参数 parameters.setPictureSize(640, 280);//预览画面尺寸 parameters.setPictureFormat(JPEG);//指定图片为JPEG parameters.set("jpeg-quality", 80);//图片的质量 camera.setParameters(parameters); camera.startPreview();//开始预览 camera.autoFocus(null);//自动对焦 } catch (IOException e) { e.printStackTrace(); } } }); pz.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { camera.takePicture(null,null,jpeg); } }); } private void resetCamera() { if(isPrivate){ camera.startPreview(); } } @Override protected void onPause() { if(camera!=null){ camera.stopPreview(); camera.release(); } super.onPause(); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/yl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="预览"/> <Button android:layout_marginLeft="100dp" android:id="@+id/pz" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照"/> <SurfaceView android:id="@+id/sufaceview1" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout> </LinearLayout>
save.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/photo_name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>