当前位置:操作系统 > 安卓/Android >>

android拍照

    拍照可以调用系统的相机进行拍照,拍完保存到相应的SD卡目录。最近要用到拍照这个功能,首先试了下调用相机拍照,保存到相应目录,然后用imageview显示的时候感觉,很模糊。可能是图片被压缩的太狠了。所以自己使用Camera写了一个拍照的功能,拍出来的还挺清晰的,不过文件有点大一张图片差不多1.7M。如果想拍高清图可以使用下面的代码。

           代码很简单,我就不做过多解释了。

         

[html]
package com.example.test易做图; 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Date; 
 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Environment; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.PixelFormat; 
import android.hardware.Camera; 
import android.text.format.DateFormat; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.Su易做图ceHolder; 
import android.view.Su易做图ceView; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
 
public class MainActivity extends Activity { 
 
    private Su易做图ceView su易做图ceView;   
    private Su易做图ceHolder su易做图ceHolder;   
    private Camera camera;   
    private File picture;   
    private Button btnSave;   
    private final static String TAG = "CameraActivity";   
    private ImageView open_picIcon; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
         
       /* Intent intent=new Intent(MainActivity.this,MyCameraActivity.class); 
        startActivity(intent); 
        finish();*/ 
        setupViews(); 
    } 
 
    private void setupViews(){   
        su易做图ceView = (Su易做图ceView) findViewById(R.id.camera_preview); // Camera inte易做图ce to instantiate components   
        su易做图ceHolder = su易做图ceView.getHolder(); // Camera inte易做图ce to instantiate components   
        su易做图ceHolder.addCallback(su易做图ceCallback); // Add a callback for the Su易做图ceHolder   
        su易做图ceHolder.setType(Su易做图ceHolder.SURFACE_TYPE_PUSH_BUFFERS);   
           
        btnSave = (Button) findViewById(R.id.save_pic); 
        open_picIcon=(ImageView) findViewById(R.id.open_picIcon); 
           
        btnSave.setOnClickListener(new OnClickListener() {   
               
            @Override   
            public void onClick(View v) { 
                su易做图ceView.setVisibility(View.VISIBLE); 
                open_picIcon.setVisibility(View.GONE); 
                takePic();   
            }   
        });   
    }   
       
    @Override   
    public boolean onKeyDown(int keyCode, KeyEvent event) {   
        if (keyCode == KeyEvent.KEYCODE_CAMERA   
                || keyCode == KeyEvent.KEYCODE_SEARCH) {   
            takePic();   
            return true;   
        }   
        return super.onKeyDown(keyCode, event);   
    }   
   
    private void takePic() {   
   
        camera.stopPreview();// stop the preview   
   
        camera.takePicture(null, null, pictureCallback); // picture   
    }   
   
    // Photo call back   
    Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {   
        //@Override   
        public void onPictureTaken(byte[] data, Camera camera) {   
            new SavePictureTask().execute(data);   
            camera.startPreview();   
        }   
    };   
   
    // save pic   
    class SavePictureTask extends AsyncTask<byte[], String, String> {   
        @Override   
        protected String doInBackground(byte[]... params) {   
            String fname = DateFormat.format("yyyyMMddhhmmss", new Date()).toString()+".jpg";   
               
            Log.i(TAG, "fname="+fname+";dir="+Environment.getExternalStorageDirectory());   补充:移动开发 , Android ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,