Android Open Source - AndroidCameraView Camera Layout






From Project

Back to project page AndroidCameraView.

License

The source code is released under:

MIT License

If you think the Android project AndroidCameraView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.tatuas.android.cameraview;
/*from   w ww.  j  a v a 2s.com*/
import java.io.File;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;

import com.tatuas.android.bitmapview.BitmapView;

public class CameraLayout extends FrameLayout {
    private CameraView cameraView;
    private BitmapView bitmapView;
    private View black;

    public CameraLayout(Context context) {
        super(context);
        setBlackBackground(context);

        cameraView = new CameraView(context);
        bitmapView = new BitmapView(context);
        setCameraGravity();

        addView(cameraView);
        setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
    }

    public CameraLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBlackBackground(context);

        cameraView = new CameraView(context, attrs);
        bitmapView = new BitmapView(context, attrs);
        setCameraGravity();

        addView(cameraView);
        setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
    }

    public CameraLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setBlackBackground(context);

        cameraView = new CameraView(context, attrs, defStyle);
        bitmapView = new BitmapView(context, attrs, defStyle);
        setCameraGravity();

        addView(cameraView);
        setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
    }

    public void setBlackBackground(Context context) {
        black = new View(context);
        black.setBackgroundResource(android.R.color.black);
    }

    public CameraView getCameraView() {
        return cameraView;
    }

    public void setCameraGravity() {
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        lp.gravity = Gravity.CENTER;
        cameraView.setLayoutParams(lp);
    }

    public void showPreview(String path) {
        if (path == null) {
            return;
        }

        File file = new File(path);
        if (!file.exists()) {
            return;
        }

        int width = getDisplayWidth();
        int height = getDisplayHeight();

        bitmapView.setImageFromFilePath(file.getAbsolutePath(), width, height);
        addView(bitmapView, 1);
    }

    public void removePreview() {
        bitmapView.setImageDrawable(null);
        removeAllViews();
        addView(cameraView);
    }

    private int getDisplayWidth() {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        return metrics.widthPixels;
    }

    private int getDisplayHeight() {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        return metrics.heightPixels;
    }

    @Override
    protected void onDetachedFromWindow() {
        removePreview();
        super.onDetachedFromWindow();
    }
}




Java Source Code List

com.tatuas.android.cameraview.AfterShutterListener.java
com.tatuas.android.cameraview.BeforeShutterListener.java
com.tatuas.android.cameraview.CameraFailedListener.java
com.tatuas.android.cameraview.CameraLayout.java
com.tatuas.android.cameraview.CameraType.java
com.tatuas.android.cameraview.CameraView.java
com.tatuas.android.cameraview.Options.java
com.tatuas.android.cameraview.PictureMaker.java
com.tatuas.android.cameraview.PictureSize.java
com.tatuas.android.cameraview.PictureType.java
com.tatuas.android.cameraview.ShutterFailedListener.java
com.tatuas.android.cameraview.Shutter.java
com.tatuas.android.cameraview.Thumbnail.java
com.tatuas.android.cameraview.Util.java
com.tatuas.android.cameraview.sample.CameraLayoutSampleActivity.java
com.tatuas.android.cameraview.sample.CameraViewSampleActivity.java