Android——拍照儲存在相簿
阿新 • • 發佈:2022-04-12
這個功能的實現使用的技術比較落後,但是對於我這樣的初學者來說還是足夠的
直接上程式碼
整個程式碼都在MainAvtivity中,測試使用的activity十分簡單,最重要的是有兩個id即可。
package com.example.takephotos; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat;import android.hardware.Camera; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.WindowManager; import android.widget.Button;import android.widget.Toast; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { private Camera camera;//攝像頭物件 private boolean isPreview=false;//是否為預覽狀態 @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//設定全屏顯示 if(!android.os.Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED )){ Toast.makeText(this,"請安裝sd卡",Toast.LENGTH_SHORT).show(); } //開啟攝像頭並預覽 SurfaceView surfaceView=findViewById(R.id.surfaceView);//用於顯示攝像頭預覽 SurfaceHolder surfaceHolder = surfaceView.getHolder(); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); Button preview=findViewById(R.id.preview); preview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(!isPreview){ camera=Camera.open();//開啟攝像頭 isPreview=true; } try { camera.setPreviewDisplay(surfaceHolder); Camera.Parameters parameters=camera.getParameters(); parameters.setPictureFormat(PixelFormat.JPEG); parameters.set("jpeg-quality",80); camera.setParameters(parameters); camera.startPreview(); camera.autoFocus(null); } catch (IOException e) { e.printStackTrace(); } } }); //實現拍照功能 Button takephoto=findViewById(R.id.takephoto); takephoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (camera!=null){ camera.takePicture(null,null,jpeg); } } }); } Camera.PictureCallback jpeg = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] bytes, Camera camera) { Bitmap bitmap= BitmapFactory.decodeByteArray(bytes,0,bytes.length); camera.stopPreview();//停止預覽 isPreview=false; File appDir=new File(Environment.getExternalStorageDirectory(),"/DCIM/Camera"); String fileName=System.currentTimeMillis()+".jpg"; File file=new File(appDir,fileName); try { FileOutputStream fos=new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //將相片插入系統圖庫 try { MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), file.getAbsolutePath(),fileName,null); } catch (FileNotFoundException e) { e.printStackTrace(); } //相簿更新 MainActivity.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+""))); Toast.makeText(MainActivity.this,"儲存至"+file,Toast.LENGTH_LONG).show(); resetCamera();//重新預覽 } }; private void resetCamera() { if(!isPreview){ camera.startPreview();//預覽 isPreview=true; } } @Override protected void onPause() { super.onPause(); if(camera!=null){ camera.stopPreview(); camera.release();//釋放資源 } } }
注:這裡面使用到了sd卡,並不一定使用真機,我使用的是逍遙模擬器(這個無論打遊戲還是學習都挺好用的)
實現效果:電腦攝像頭是前置的