この記事を検索で来たいという事は、以下の情報は、既知のはず。
「#13 やってみよう画像処理『カメラのbyte列をいじる』」
これと、このベースとなっている、
「Developerサイトの Cameraの解説」
この記事と。
先の記事では、
Developerサイトのサンプルをベースに進めます。
Cameraの解説
SurfaceViewでリアルタイムにカメラのプレビューが見れる状態(Capturing picturesの手前)まで 実装した状態から、拡張していきます。
とある。
ここで、この話題は、Android Studio の、
「Start a new Android Studio project 」
を使って、「 EmptyActivity 」を作って動かせる人が、前提です。
EmptyActivity ができても、動かすのは大変、では無いです。難しいのは、class CameraPreview のクラスのところとかだと思います(私はそうだった)。
で、どうするのかというと、全てを、
「 class CameraActivity をインナークラスとして使う」
これで、超、簡単になる。
えっと、こんな感じ。
public class MyCamera extends AppCompatActivity {
private Camera mCamera;
private CameraPreview mPreview;
private final String TAG="[MC]";
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
/** Check if this device has a camera */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
// this device has a camera
return true;
} else {
// no camera on this device
return false;
}
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_camera);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
}
あと、やったのは、ボタンが横につくのがいやだったので、LinearLayout の android:orientation を vertical にした。
この状態でコンパイルすると、ボタンが縦について、動く。
これ、このサンプル、ここまでは意外に簡単にちゃんと動いてしまって、ビックリ。
次は、ビデオフレームをバイト列で取る方法だけど、これも、ちょっと違ったので、別便で。