使用surfaceview进行预览时,相机肖像不会全尺寸

时间:2015-01-28 11:01:46

标签: android android-camera surfaceview

请帮帮我

我正在创建带有方向肖像的应用相机,但是预览相机不是全尺寸到屏幕,出了什么问题?

这是我的班级: CameraPreview.class

package com.dm.zbar.android.scanner;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.Size;
import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import java.io.IOException;
import java.util.List;


class CameraPreview extends ViewGroup implements SurfaceHolder.Callback {
    private final String TAG = "CameraPreview";

    SurfaceView mSurfaceView;
    SurfaceHolder mHolder;
    Size mPreviewSize, mPictureSize;
    List<Size> mSupportedPreviewSizes;
    Camera mCamera;
    PreviewCallback mPreviewCallback;
    AutoFocusCallback mAutoFocusCallback;



    CameraPreview(Context context, PreviewCallback previewCallback, AutoFocusCallback autoFocusCb) {
        super(context);

        mPreviewCallback = previewCallback;
        mAutoFocusCallback = autoFocusCb;
        mSurfaceView = new SurfaceView(context);
        addView(mSurfaceView);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


    }

    public void setCamera(Camera camera) {
        mCamera = camera;
        if (mCamera != null) {
            mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
            requestLayout();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // We purposely disregard child measurements because act as a
        // wrapper to a SurfaceView that centers the camera preview instead
        // of stretching it.
        final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);

        if (mSupportedPreviewSizes != null) {
            mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed && getChildCount() > 0) {
            final View child = getChildAt(0);

            final int width = r - l;
            final int height = b - t;

            int previewWidth = width;
            int previewHeight = height;
            if (mPreviewSize != null) {
                previewWidth = mPreviewSize.width;
                previewHeight = mPreviewSize.height;
            }

            // Center the child SurfaceView within the parent.
            if (width * previewHeight > height * previewWidth) {
                final int scaledChildWidth = previewWidth * height / previewHeight;
                child.layout((width - scaledChildWidth) / 2, 0,
                        (width + scaledChildWidth) / 2, height);
            } else {
                final int scaledChildHeight = previewHeight * width / previewWidth;
                child.layout(0, (height - scaledChildHeight) / 2,
                        width, (height + scaledChildHeight) / 2);
            }
        }
    }

    public void hideSurfaceView() {
        mSurfaceView.setVisibility(View.INVISIBLE);
    }

    public void showSurfaceView() {
        mSurfaceView.setVisibility(View.VISIBLE);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        try {
            if (mCamera != null) {
                mCamera.setPreviewDisplay(holder);
            }
        } catch (IOException exception) {
            Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        if (mCamera != null) {
            mCamera.cancelAutoFocus();
            mCamera.stopPreview();
        }
    }


    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null) return null;

        Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        if (holder.getSurface() == null){
          // preview surface does not exist
          return;
        }

        if (mCamera != null) {
            // Now that the size is known, set up the camera parameters and begin
            // the preview.
            Camera.Parameters parameters = mCamera.getParameters();
            parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);

            if(getResources().getConfiguration().orientation== getResources().getConfiguration().ORIENTATION_LANDSCAPE){

                mCamera.setDisplayOrientation(0);
            }else{
                mCamera.setDisplayOrientation(90);
            }

            requestLayout();

            mCamera.setParameters(parameters);
            mCamera.setPreviewCallback(mPreviewCallback);
            mCamera.startPreview();
            mCamera.autoFocus(mAutoFocusCallback);
        }
    }

}

这是我的捕获: http://postimg.org/image/hrrmrto3f/

http://postimg.org/image/obgq3g9vp/

2 个答案:

答案 0 :(得分:0)

对我来说很好。将此添加到mainactivity:

mPreview = new CamLayer(this);
Splash.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            if (mPreview == null) {
                mPreview = new CamLayer(MainActivity.this);
            }
            mPreview.setcameraparam(flash);
            Intent i = new Intent(MainActivity.this, ShowingImage.class);
            mPreview.getimage(MainActivity.this, i);

        }
    });

然后在你的公共classCamLayer中扩展SurfaceView实现SurfaceHolder.Callback类,添加以下方法:

public void getimage(final Context context,final Intent i) {
    final File pictureFile = getOutputMediaFile();

    PictureCallback mPicture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            if (pictureFile == null) {
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
                i.putExtra("file_path", pictureFile.getPath());
                context.startActivity(i);
            } catch (FileNotFoundException e) {
                Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
            }
        }
    };
    mCamera.takePicture(null, null, mPicture);

}

private static File getOutputMediaFile() {
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "Sunil2");
    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;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
}

public void setcameraparam(String param) {
    Parameters parameter = mCamera.getParameters();
    parameter.setFlashMode(param);
    mCamera.setParameters(parameter);
}

并在意图后显示图像类:

public class ShowingImage extends Activity {
@SuppressLint("NewApi")
Bitmap myBitmap;
int width;
int height;

private float curScale = 1F;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.show_image);
    Intent intent = getIntent();
    String path = intent.getStringExtra("file_path");
    Log.e("file store in ", "" + path);
    File imgFile = new File(path);

    if (imgFile.exists()) {

        ImageView myImage = (ImageView) findViewById(R.id.imageView1);
        myBitmap = BitmapFactory.decodeFile(path);
        width = myBitmap.getWidth();
        height = myBitmap.getHeight();

        Matrix matrix = new Matrix();
         matrix.postScale(curScale, curScale);
        matrix.postRotate(90);

        Bitmap resizedBitmap = Bitmap.createBitmap(myBitmap, 0, 0, width,
                height, matrix, true);
        myImage.setImageBitmap(resizedBitmap);

    } else {
        Toast.makeText(getApplicationContext(), "file not exist", 1000)
                .show();
    }
}

}

的xml:

<ImageView 
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />

答案 1 :(得分:0)

我猜你的.xml不正确:用这个替换你的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<RelativeLayout
        android:id="@+id/top_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       >

        <TextView
            android:id="@+id/dashboard_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:textSize="16sp"
            android:text="Front of the card"
            android:textColor="#8f928c" />

    </RelativeLayout>


    <FrameLayout
            android:id="@+id/camera_preview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/btn_area"
            android:layout_below="@+id/top_bar" />


    <LinearLayout 
        android:id="@+id/btn_area"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_vertical"
        android:layout_margin="5dp"
        android:paddingBottom="3.5dp"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/button_preview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"

            android:enabled="false"
            android:textSize="18sp"
            android:text="Refresh" />

        <Button
            android:id="@+id/button_capture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"

            android:enabled="false"
            android:textSize="18sp"
            android:text="Capture" />

        <Button
            android:id="@+id/button_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"

            android:enabled="false"
            android:textSize="18sp"
            android:text="Ok" />

    </LinearLayout>   

</RelativeLayout>

您的 CameraActivity.java

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;

public class CameraActivity extends Activity {
    private Camera mCamera;
    private CustomizeCameraPreview mPreview;
    public static final int MEDIA_TYPE_IMAGE = 1;
    Button captureButton;
    Button previewButton;
    Button viewButton;
    String pictureFilePath = null;
    FrameLayout preview;
    TextView tvheader;
    //addition for free-hand drawing
    Drawing mv = null;
    int color_code = 0xFFFF2525;
    ImageView colorButton;
    ImageView undo;
    boolean startDraw = false;
    String str="";


    private PictureCallback mPicture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
            // TODO Auto-generated method stub

            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if (pictureFile == null){
                Log.d("CameraActivity", "Error creating media file, check storage permissions");
                return;
            }

            initDrawing(scale_and_rotateImage(data,480));
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customcamera);
        //str=getIntent().getExtras().getString("cardimage");
        try{
        str=getIntent().getStringExtra("cardimage");
        System.out.println("str==="+str);
        tvheader=(TextView)findViewById(R.id.dashboard_logo);
        if(str.equals("front"))
            tvheader.setText("Front of the card");
        else
            tvheader.setText("Back of the card");
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        startDraw = true;
    }


    @Override
    public void onResume() {
        // 31-Jul-2013 ///////////////////
        preview = (FrameLayout) findViewById(R.id.camera_preview);
        // Add a listener to the Preview button


        previewButton = (Button) findViewById(R.id.button_preview);
        captureButton = (Button) findViewById(R.id.button_capture);
        viewButton = (Button) findViewById(R.id.button_view);

        if(checkCameraHardware(getApplicationContext())) {
            if (mCamera != null) {
                mCamera.release();
                mCamera = null;
            }
            mCamera = getCameraInstance();
            flashLightOn(mCamera);

            // Create our Preview view and set it as the content of our activity.
            mPreview = new CustomizeCameraPreview(getApplicationContext(), mCamera);
            preview.addView(mPreview);
            previewButton.setEnabled(false);
            captureButton.setEnabled(true);
            viewButton.setEnabled(false);
        }

        previewButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startDraw = true;
                    // release previous camera instance
                    if (mCamera != null) {
                        mCamera.stopPreview();
                        preview.removeView(mPreview);
                        mCamera.release();
                        mCamera = null;
                    }
                    if(mPreview != null) mPreview = null;
                    // Create an instance of Camera
                    mCamera = getCameraInstance();
                    flashLightOn(mCamera);

                    // Create our Preview view and set it as the content of our activity.
                    mPreview = new CustomizeCameraPreview(getApplicationContext(), mCamera);

                    preview.addView(mPreview);
                    v.setEnabled(false);
                    captureButton.setEnabled(true);
                    viewButton.setEnabled(false);
                }
            }
        );

        // Add a listener to the Capture button
        captureButton.setOnClickListener (
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // get an image from the camera
                    try {
                    mCamera.takePicture(null, null, mPicture);
                    previewButton.setEnabled(true);
                    v.setEnabled(false);
                    viewButton.setEnabled(true);
                    }catch(Exception e) {
                        //mCamera not instantiated
                    }
                }
            }
        );

        // Add a listener to the View button
        viewButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // return image path to calling activity
                    pictureFilePath = saveImage();
                    if(pictureFilePath != null) {
                        Intent data = new Intent();
                        //---set the data to pass back---
                        data.setData(Uri.parse(pictureFilePath));
                        setResult(RESULT_OK, data);
                        //---closes the activity---
                        finish();
                    }   
                    previewButton.setEnabled(true);
                    captureButton.setEnabled(false);
                    //v.setEnabled(false);
                }
            }
        );

        //////////////////////////////////
        super.onResume();
    }

    public String saveImage() {
        pictureFilePath = null;
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
            Log.d("CameraActivity", "Error creating media file, check storage permissions");
            return null;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            Bitmap tmp = mv.getDrawingCache();
            tmp.compress(Bitmap.CompressFormat.JPEG, 90, fos);
            fos.flush();
            fos.close();
            pictureFilePath = pictureFile.getAbsolutePath();
        } catch (Exception e) {
            Log.d("CameraActivity", "Error accessing file: " + e.getMessage());
        }
        return pictureFilePath;
    }


    /** 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;
        }
    }

    public void flashLightOn(Camera cam) {
        try {
            if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_AUTO);
                cam.setParameters(p);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //Toast.makeText(getBaseContext(), "Exception flashLightOn()", Toast.LENGTH_SHORT).show();
        }
    }

    /** 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
            c.setDisplayOrientation(90);
            //context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }

    /** Create a file Uri for saving an image or video */
    private static Uri getOutputMediaFileUri(int type) {
          return Uri.fromFile(getOutputMediaFile(type));
    }

    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(int type) {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.
        /*
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES), "MyCameraApp");
        */
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM), "canberra trailpass");

        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("canberra trailpass", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        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;
    }

    private Bitmap scale_and_rotateImage(byte[] input, int dstWidth) 
    {
        // code to free some memory
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;

        // create the blank bitmap
        Bitmap inputImage = BitmapFactory.decodeByteArray(input, 0, input.length, opts); // java.lang.OutOfMemoryError error

        // create the smaller bitmap
        inputImage = null;
        opts.inJustDecodeBounds = false;
        opts.inSampleSize = (int) opts.outWidth / dstWidth;
        opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
        inputImage = BitmapFactory.decodeByteArray(input, 0, input.length, opts);

        // create a matrix for the manipulation
        Matrix m = new Matrix();

        //int width = inputImage.getWidth();
        //int height = inputImage.getHeight();
        int width = opts.outWidth;
        int height = opts.outHeight;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) dstWidth) / width;
        //float scaleHeight = ((float) dstHeight) / height;

        // resize the bit map
        m.postScale(scaleWidth, scaleWidth);
        // rotate the Bitmap
        m.postRotate(90);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(inputImage, 0, 0,
                          width, height, m, true);

        inputImage.recycle();
        inputImage = null;
        /*
        ByteArrayOutputStream blob = new ByteArrayOutputStream();
        resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, blob);

        resizedBitmap.recycle();
        resizedBitmap = null;

        return blob.toByteArray();
        */
        return resizedBitmap;
    }

    @Override
    public void onBackPressed(){
        Intent data = new Intent();
        //---set the data to pass back---
        //data.setData(null);
        System.out.println("--> Back from camera activity");
        setResult(RESULT_CANCELED, data);
        finish();
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        preview.removeView(mPreview);
        super.onPause();
    }

    public void initDrawing(Bitmap resizedBitmap) {
        int line_width = (int) this.getResources().getInteger(R.integer.line_width);
        mv = new Drawing(this,line_width,color_code,1);
        mv.setDrawingCacheEnabled(true);
        //mv.setBackground(new BitmapDrawable(resizedBitmap));
        mv.setBackgroundDrawable(new BitmapDrawable(getResources(), resizedBitmap));
        preview.addView(mv);

        LinearLayout bt_container = new LinearLayout(this);
        bt_container.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        bt_container.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(5, 2, 5, 2);

        undo = new ImageView(this);
        undo.setImageResource(R.drawable.undo_icon);
        bt_container.addView(undo,layoutParams);
        undo.setVisibility(View.GONE);
        undo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mv.onClickUndo();
            }
        });

       /* colorButton = new ImageView(this);
        //colorButton.setImageResource(R.drawable.pencil1); changed on 25-Nov-13
        colorButton.setBackgroundColor(color_code);
        colorButton.setImageResource(R.drawable.pencil2);

        bt_container.addView(colorButton,layoutParams);
        colorButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AmbilWarnaDialog dialog = new AmbilWarnaDialog(CameraActivity.this, color_code, new OnAmbilWarnaListener() {
                    @Override
                    public void onOk(AmbilWarnaDialog dialog, int color) {
                        startDraw = true;
                        color_code = color;
                        mv.setColor(color_code);
                        colorButton.setBackgroundColor(color_code);
                        colorButton.setImageResource(R.drawable.pencil2);
                    }

                    @Override
                    public void onCancel(AmbilWarnaDialog dialog) { 
                    }
                });
                dialog.show();
            }
        });*/

        FrameLayout.LayoutParams fParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, 
                Gravity.RIGHT | Gravity.TOP);

        preview.addView(bt_container, fParams);
    }

    public class Drawing extends View {
        private Paint mPaint, mBitmapPaint;
        private Bitmap mBitmap;
        private Canvas mCanvas;
        private Path mPath;
        private Context mContext;

        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private int color, size, state;

        private ArrayList<Path> paths = new ArrayList<Path>();
        private ArrayList<Path> undonePaths = new ArrayList<Path>();
        private ArrayList<Integer> colors = new ArrayList<Integer>();
        private ArrayList<Integer> sizes = new ArrayList<Integer>();

        public Drawing(Context c) {
            super(c);
            mContext = c;
        }

        public Drawing(Context c, int size, int color, int state) {
            super(c);
            mContext = c;

            mPath = new Path();

            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(color);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(size);        
            setColor(color);
            setSize(size);
            setState(state);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);

            mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            if(!startDraw) return;

            for (int i = 0; i < paths.size(); i++) {
                mPaint.setColor(colors.get(i));
                mPaint.setStrokeWidth(sizes.get(i));
                canvas.drawPath(paths.get(i), mPaint);
            }
            mPaint.setColor(color);
            canvas.drawPath(mPath, mPaint);
        }

        public void setColor(int color) {
            this.color = color;
            mPaint.setColor(color);
        }

        public void setSize(int size) {
            this.size = size;
        }

        public void setState(int state) {
            this.state = state;
            paths.clear();
            sizes.clear();
            colors.clear();
            invalidate();
            mPaint.setXfermode(null);
            //mPaint.setAlpha(0xFF);
            mPaint.setStrokeWidth(size);
        }

        public void clearCanvas() {
            mBitmap.eraseColor(Color.TRANSPARENT);
            paths.clear();
            sizes.clear();
            colors.clear();
            mPath.reset();
            invalidate();
        }

        public void onClickUndo() {
            if (paths.size() > 0) {
                undonePaths.add(paths.remove(paths.size() - 1));
                sizes.remove(sizes.size() - 1);
                colors.remove(colors.size() - 1);
                if(colors.size() > 0)
                    undo.setBackgroundColor(colors.get(colors.size() - 1));
                else
                    undo.setVisibility(View.GONE);
                invalidate();
            }
        }

        private void touch_start(float x, float y) {
            undo.setVisibility(View.VISIBLE);
            undo.setBackgroundColor(color);
            undonePaths.clear();
            mPath.reset();
            //colors.add(color);
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }

        private void touch_move(float x, float y) {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
                mX = x;
                mY = y;
            }
        }

        private void touch_up() {
            mPath.lineTo(mX, mY);
            mCanvas.drawPath(mPath, mPaint);
            if(state != 0) {
                colors.add(color);
                sizes.add(size);
                paths.add(mPath);
                mPath = new Path();
            }
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if(!startDraw) return false;

            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
            }
            return true;
        }
    }
}

我在 CameraPreview.java 中找不到任何错误 输出如下:

enter image description here