服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android实现简单拍照功能

android实现简单拍照功能

2022-03-07 14:53wangkuifeng0118 Android

这篇文章主要为大家详细介绍了android实现简单拍照功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.example.testsimple;
 
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.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
 
public class MainActivity extends Activity {
 
 private SurfaceView surfaceView;
  private SurfaceHolder surfaceHolder;
  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(){
    surfaceView = (SurfaceView) findViewById(R.id.camera_preview); // Camera interface to instantiate components
    surfaceHolder = surfaceView.getHolder(); // Camera interface to instantiate components
    surfaceHolder.addCallback(surfaceCallback); // Add a callback for the SurfaceHolder
    surfaceHolder.setType(SurfaceHolder.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) {
       surfaceView.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());
      //picture = new File(Environment.getExternalStorageDirectory(),fname);// create file
       
      picture = new File(Environment.getExternalStorageDirectory()+"/images/"+fname);
      File file= picture.getParentFile();
      if(!file.exists())
           file.mkdir();
      try {
        FileOutputStream fos = new FileOutputStream(picture.getPath()); // Get file output stream
        fos.write(params[0]); // Written to the file
        fos.close(); 
      } catch (Exception e) {
        e.printStackTrace();
      }
      return null;
    }
    @Override
    protected void onPostExecute(String result) {
     // TODO Auto-generated method stub
     super.onPostExecute(result);
     
     Bitmap bitmap=getDiskBitmap(picture.getPath());
     surfaceView.setVisibility(View.GONE);
     open_picIcon.setVisibility(View.VISIBLE);
     open_picIcon.setImageBitmap(bitmap);
    }
    
  }
  private Bitmap getDiskBitmap(String pathString)
  {
    Bitmap bitmap = null;
    try
    {
      File file = new File(pathString);
      if(file.exists())
      {
        bitmap = BitmapFactory.decodeFile(pathString);
      }
    } catch (Exception e)
    {
      // TODO: handle exception
    }
     
     
    return bitmap;
  }
  // SurfaceHodler Callback handle to open the camera, off camera and photo size changes
  SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
 
    public void surfaceCreated(SurfaceHolder holder) {
      Log.i(TAG, "surfaceCallback====");
      camera = Camera.open(); // Turn on the camera
      try {
        camera.setPreviewDisplay(holder); // Set Preview
      } catch (IOException e) {
        camera.release();// release camera
        camera = null;
      }
    }
 
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
      Log.i(TAG,"====surfaceChanged");
      Camera.Parameters parameters = camera.getParameters(); // Camera parameters to obtain
      parameters.setPictureFormat(PixelFormat.JPEG);// Setting Picture Format
//     parameters.set("rotation", 180); // Arbitrary rotation
      camera.setDisplayOrientation(0);
      parameters.setPreviewSize(400, 300); // Set Photo Size
      camera.setParameters(parameters); // Setting camera parameters
      camera.startPreview(); // Start Preview
    }
 
    public void surfaceDestroyed(SurfaceHolder holder) {
      Log.i(TAG,"====surfaceDestroyed");
      camera.stopPreview();// stop preview
      camera.release(); // Release camera resources
      camera = null;
    }
  };
}

拍完以后自动保存到SD卡中的images目录下 

android实现简单拍照功能

下面是图片存放路径

android实现简单拍照功能

1.8M 确实有点大,不过是高清图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/wangkuifeng0118/article/details/8190935

延伸 · 阅读

精彩推荐