2017-06-02 13 views

答えて

1

件までマシュマロでデフォルトとして、フロントカメラはオープンしたい...

package com.oreillyschool.android2.cameraadvanced; 

import java.io.IOException; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.hardware.Camera; 
import android.hardware.Camera.PictureCallback; 
import android.os.Bundle; 
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; 
import android.widget.Toast; 

public class CameraActivity extends Activity implements PictureCallback, SurfaceHolder.Callback { 

    public static final String EXTRA_CAMERA_DATA = "camera_data"; 

    private static final String KEY_IS_CAPTURING = "is_capturing"; 

    private Camera mCamera; 
    private ImageView mCameraImage; 
    private SurfaceView mCameraPreview; 
    private Button mCaptureImageButton; 
    private byte[] mCameraData; 
    private boolean mIsCapturing; 

    private OnClickListener mCaptureImageButtonClickListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     captureImage(); 
    } 
    }; 

    private OnClickListener mRecaptureImageButtonClickListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     setupImageCapture(); 
    } 
    }; 

    private OnClickListener mDoneButtonClickListener = new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if (mCameraData != null) { 
     Intent intent = new Intent(); 
     intent.putExtra(EXTRA_CAMERA_DATA, mCameraData); 
     setResult(RESULT_OK, intent); 
     } else { 
     setResult(RESULT_CANCELED); 
     } 
     finish(); 
    } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_camera); 

    mCameraImage = (ImageView) findViewById(R.id.camera_image_view); 
    mCameraImage.setVisibility(View.INVISIBLE); 

    mCameraPreview = (SurfaceView) findViewById(R.id.preview_view); 
    final SurfaceHolder surfaceHolder = mCameraPreview.getHolder(); 
    surfaceHolder.addCallback(this); 
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    mCaptureImageButton = (Button) findViewById(R.id.capture_image_button); 
    mCaptureImageButton.setOnClickListener(mCaptureImageButtonClickListener); 

    final Button doneButton = (Button) findViewById(R.id.done_button); 
    doneButton.setOnClickListener(mDoneButtonClickListener); 

    mIsCapturing = true; 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 

    savedInstanceState.putBoolean(KEY_IS_CAPTURING, mIsCapturing); 
    } 

    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 

    mIsCapturing = savedInstanceState.getBoolean(KEY_IS_CAPTURING, mCameraData == null); 
    if (mCameraData != null) { 
     setupImageDisplay(); 
    } else { 
     setupImageCapture(); 
    } 
    } 

    @Override 
    protected void onResume() { 
    super.onResume(); 

    if (mCamera == null) { 
     try { 
     mCamera = Camera.open();    
     mCamera.setPreviewDisplay(mCameraPreview.getHolder()); 
     if (mIsCapturing) { 
      mCamera.startPreview(); 
     } 
     } catch (Exception e) { 
     Toast.makeText(CameraActivity.this, "Unable to open camera.", Toast.LENGTH_LONG) 
     .show(); 
     } 
    } 
    } 

    @Override 
    protected void onPause() { 
    super.onPause(); 

    if (mCamera != null) { 
     mCamera.release(); 
     mCamera = null; 
    } 
    } 

    @Override 
    public void onPictureTaken(byte[] data, Camera camera) { 
    mCameraData = data; 
    setupImageDisplay(); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
    if (mCamera != null) { 
     try { 
     mCamera.setPreviewDisplay(holder); 
     if (mIsCapturing) { 
      mCamera.startPreview(); 
     } 
     } catch (IOException e) { 
     Toast.makeText(CameraActivity.this, "Unable to start camera preview.", Toast.LENGTH_LONG).show(); 
     } 
    } 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
    } 

    private void captureImage() { 
    mCamera.takePicture(null, null, this); 
    } 

    private void setupImageCapture() { 
    mCameraImage.setVisibility(View.INVISIBLE); 
    mCameraPreview.setVisibility(View.VISIBLE); 
    mCamera.startPreview(); 
    mCaptureImageButton.setText(R.string.capture_image); 
    mCaptureImageButton.setOnClickListener(mCaptureImageButtonClickListener); 
    } 

    private void setupImageDisplay() { 
    Bitmap bitmap = BitmapFactory.decodeByteArray(mCameraData, 0, mCameraData.length); 
    mCameraImage.setImageBitmap(bitmap); 
    mCamera.stopPreview(); 
    mCameraPreview.setVisibility(View.INVISIBLE); 
    mCameraImage.setVisibility(View.VISIBLE); 
    mCaptureImageButton.setText(R.string.recapture_image); 
    mCaptureImageButton.setOnClickListener(mRecaptureImageButtonClickListener); 
    } 
} 
関連する問題