1
votes

I follow https://developer.android.com/guide/topics/media/camera.html to activate the custom camera for android 4 but my capture function are totally not working. Below is my code:

cameraf.java:

public class cameraf extends AppCompatActivity{
private Preview mPreview;
private Camera mCamera;
private static final String TAG = "Myact";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cameraf);
    mCamera = getCameraInstance();
    mPreview = new Preview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
   preview.setOnTouchListener(new View.OnTouchListener(){
        public boolean onTouch(View v, MotionEvent event){
            Log.v(TAG, "will now take picture");
            mCamera.takePicture(null, null, mPicture);
            return true;
        }
    });
    preview.addView(mPreview);
}
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.v(TAG, "Getting output media file");
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null) {
            Log.v(TAG, "Error creating output file");
            return;}
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.v(TAG, e.getMessage());
        } catch (IOException e) {
            Log.v(TAG, e.getMessage());}
    }
};
public static final int MEDIA_TYPE_IMAGE = 1;
private static File getOutputMediaFile(int type){
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "MyCameraApp");
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;}
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;}
    return mediaFile;
}
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
    }
    return c; // returns null if camera is unavailable
}
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    public Preview(Context context, Camera camera) {
        super(context);
        mCamera = camera;
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.setDisplayOrientation(90);
            Camera.Parameters params = mCamera.getParameters();
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            mCamera.setParameters(params);
        } catch (IOException exception) {
            mCamera.release();
            mCamera = null;}
    }
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.startPreview();
    }
}
private void releaseCamera(){
    if (mCamera != null){
        mCamera.release();        // release the camera for other applications
        mCamera = null;
    }
}
}

And I have add the permission in manifest which is uses-permission android:name="android.permission.CAMERA",uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

But from above code it able let me open the camera but when i tap on the screen it become error and cannot store the image. So anyone can share me ideas?

Error: enter image description here

1
add android:name="android.permission.READ_EXTERNAL_STORAGE" and please show the stacktrace of the error you are getting. - user6327816
Yes, i have add already but problem still same so i remove before i post this forum, about the error is shown in my phone,which write Unfortunately Camera apps has stopped - Shi Jie Tio
there is a logcat section in Android-Studio, you can have a look at the stacktrace there. - user6327816
Hi i have upload my error from logcat there - Shi Jie Tio
@RahulKumar hi, do you have any ideas? if can please share me ideas, thanks, this make me turn into trouble - Shi Jie Tio

1 Answers

0
votes

The failure you see could happen because you do not set picture size. Some devices require this.

Note that you must choose one of supported sizes, and also make sure that the picture size and the preview size are in sync (i.e. have same aspect ratio).

You cannot set preview size derived from the surface view size … actually, you don't: your call

parameters.setPreviewSize(w, h);

is not followed by

mCamera.setParameters(parameters);