Camera camera;
camera = Camera.open();
camera.release();
SurfaceView preview;
SurfaceHolder surfaceHolder; surfaceHolder = preview.getHolder(); camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
void takePicture(Camera.ShutterCalback shutter, Camera.PictureCallback raw, Camera.PictureCallback postview, Camera.PictureCallback jpg);
public void onAutoFocus(boolean paramBoolean, Camera paramCamera);
surfaceHolder.addCallback();
public void surfaceCreated(SurfaceHolder holder); public void surfaceChanged(SurfaceHolder holder, int format, int width, int height); public void surfaceDestroyed(SurfaceHolder holder);
LayoutParams lp = preview.getLayoutParams(); lp.width = ; lp.height = ; preview.setLayoutParams(lp);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
camera.setDisplayOrientation(0)
. void onPreviewFrame(byte[] paramArrayOfByte, Camera paramCamera);
<uses-permission android:name="android.permission.CAMERA" />
package test.camera; import android.app.Activity; import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.view.View; import android.hardware.Camera; import android.hardware.Camera.Size; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class MainScreen extends Activity implements SurfaceHolder.Callback, View.OnClickListener, Camera.PictureCallback, Camera.PreviewCallback, Camera.AutoFocusCallback { private Camera camera; private SurfaceHolder surfaceHolder; private SurfaceView preview; private Button shotBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // , setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // , getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); // requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); // SurfaceView SurfaceView01 preview = (SurfaceView) findViewById(R.id.SurfaceView01); surfaceHolder = preview.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // Button01 shotBtn = (Button) findViewById(R.id.Button01); shotBtn.setText("Shot"); shotBtn.setOnClickListener(this); } @Override protected void onResume() { super.onResume(); camera = Camera.open(); } @Override protected void onPause() { super.onPause(); if (camera != null) { camera.setPreviewCallback(null); camera.stopPreview(); camera.release(); camera = null; } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { try { camera.setPreviewDisplay(holder); camera.setPreviewCallback(this); } catch (IOException e) { e.printStackTrace(); } Size previewSize = camera.getParameters().getPreviewSize(); float aspect = (float) previewSize.width / previewSize.height; int previewSurfaceWidth = preview.getWidth(); int previewSurfaceHeight = preview.getHeight(); LayoutParams lp = preview.getLayoutParams(); // preview, if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { // camera.setDisplayOrientation(90); lp.height = previewSurfaceHeight; lp.width = (int) (previewSurfaceHeight / aspect); ; } else { // camera.setDisplayOrientation(0); lp.width = previewSurfaceWidth; lp.height = (int) (previewSurfaceWidth / aspect); } preview.setLayoutParams(lp); camera.startPreview(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } @Override public void onClick(View v) { if (v == shotBtn) { // // //camera.takePicture(null, null, null, this); camera.autoFocus(this); } } @Override public void onPictureTaken(byte[] paramArrayOfByte, Camera paramCamera) { // jpg /sdcard/CameraExample/ // - System.currentTimeMillis() try { File saveDir = new File("/sdcard/CameraExample/"); if (!saveDir.exists()) { saveDir.mkdirs(); } FileOutputStream os = new FileOutputStream(String.format("/sdcard/CameraExample/%d.jpg", System.currentTimeMillis())); os.write(paramArrayOfByte); os.close(); } catch (Exception e) { } // , , . paramCamera.startPreview(); } @Override public void onAutoFocus(boolean paramBoolean, Camera paramCamera) { if (paramBoolean) { // , paramCamera.takePicture(null, null, null, this); } } @Override public void onPreviewFrame(byte[] paramArrayOfByte, Camera paramCamera) { // , preview } }
<?xml version="1.0" encoding="utf-8"?> <FrameLayout android:id="@+id/FrameLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <SurfaceView android:id="@+id/SurfaceView01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </SurfaceView> <Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> </FrameLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.camera" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name=".MainScreen" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest>
Source: https://habr.com/ru/post/112272/
All Articles