我想在surfaceview上绘制一个矩形并在矩形内裁剪图像

时间:2016-04-07 05:51:45

标签: android android-camera android-canvas surfaceview

以下是我想在表面视图上绘制矩形并在其中裁剪图像的代码

我从相机中捕获图像,然后尝试裁剪矩形内部可见的部分,但没有运气,我能够裁剪,但这是不合适的,请帮忙!提前致谢

package com.desmond.demo.camera;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.desmond.demo.R;

public class CameraFragment extends Fragment implements SurfaceHolder.Callback, Camera.PictureCallback {


    int left , top, right, bottom;
    Rect rect;
    public static Fragment newInstance() {
        return new CameraFragment();
    }

    public CameraFragment() {}



    private class DrawOnTop extends View 
    { 
        public DrawOnTop(Context context) 
        { 
            super(context); 
        } 

        @Override 
        public void onDraw(Canvas canvas) 
        {
            if (bitmap != null) 
            {
                overlayX = (canvas.getWidth()/2)-(bitmap.getWidth()/2);
                overlayY =  canvas.getHeight()/4;

//              canvas.drawBitmap(bitmap, overlayX, overlayY, null);

                Paint paint = new Paint();
                paint.setColor(Color.RED);
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(5);
                paint.setAntiAlias(true);

                left = (int)overlayX;
                top = (int)overlayY;
                right = (int)overlayX+bitmap.getWidth();
                bottom = (int)overlayY+bitmap.getHeight();

                rect = new Rect(left, top, right, bottom);
                canvas.drawRect(rect,paint);
            }
            super.onDraw(canvas); 
        } 
    }


    /**
     * Take a picture
     */
    private void takePicture() {

        if (mIsSafeToTakePhoto) {
            setSafeToTakePhoto(false);

            mOrientationListener.rememberOrientation();

            // Shutter callback occurs after the image is captured. This can
            // be used to trigger a sound to let the user know that image is taken
            Camera.ShutterCallback shutterCallback = null;

            // Raw callback occurs when the raw image data is available
            Camera.PictureCallback raw = null;

            // postView callback occurs when a scaled, fully processed
            // postView image is available.
            Camera.PictureCallback postView = null;

            // jpeg callback occurs when the compressed image is available
            mCamera.takePicture(shutterCallback, raw, postView, this);
        }
    }


    /**
     * A picture has been taken
     * @param data
     * @param camera
     */
    @Override
    public void onPictureTaken(byte[] data, Camera camera) 
    {
         int rotation = getPhotoRotation();

         // TODO Auto-generated method stub   
         Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

         int wid = cameraBitmap.getWidth();
         int hgt = cameraBitmap.getHeight();

        //  Toast.makeText(getApplicationContext(), wid+""+hgt, Toast.LENGTH_SHORT).show();
         Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);

         Canvas canvas = new Canvas(newImage);

         canvas.drawBitmap(cameraBitmap, 0f, 0f, null);

         Drawable drawable = getResources().getDrawable(R.drawable.ring_image);
       drawable.setBounds((canvas.getWidth()/2)-(drawable.getIntrinsicWidth()/2), canvas.getHeight()/4, drawable.getIntrinsicWidth()+((canvas.getWidth()/2)-(drawable.getIntrinsicWidth()/2)), drawable.getIntrinsicHeight()+canvas.getHeight()/4);

         drawable.draw(canvas);

         File storagePath = new File(Environment.getExternalStorageDirectory() + "/DiamondValuer/"); 
         storagePath.mkdirs(); 

         try
         {
            Matrix matrix = new Matrix();
            File originalImage = new File(storagePath,"original.jpg");
            FileOutputStream out = new FileOutputStream(originalImage);
            cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);

            out.flush();
            out.close();

            // Somewhere in your code
            // 2.1 Load bitmap from your .jpg file              
            Bitmap bitmap = BitmapFactory.decodeFile(originalImage.getPath());

            // 2.2 Rotate the bitmap to be the same as display, if need.
//          ... Add some bitmap rotate code
//          bitmap = RotateBitmap(bitmap, 90);
            // 2.3 Size of rotated bitmap
            int bitWidth = bitmap.getWidth();
            int bitHeight = bitmap.getHeight();

            // 3. Size of camera preview on screen  
            int preWidth = cameraBitmap.getWidth();
            int preHeight = cameraBitmap.getHeight();


            int startx = left * bitWidth / preWidth;
            int starty = top * bitHeight / preHeight;
            int endx = (preWidth - right) * (bitWidth / preWidth);
            int endy = (preHeight - bottom) * (bitHeight / preHeight);

            Log.e("TAG", "START X = "+startx+" \nSTART Y ="+starty+"\nEND X="+endx+" \nEND Y="+endy);
            Log.e("TAG", "LEFT = "+left+" \nTOP ="+top+"\nRIGHT="+right+" \nBOTTOM="+bottom);
            Log.e("TAG", "BITMAP WIDTH = "+bitmap.getWidth());

            // 5. Crop image
            Bitmap blueArea = Bitmap.createBitmap(bitmap, startx, starty, endx, endy);

            File cropped1 = new File(storagePath, Long.toString(System.currentTimeMillis()) + ".jpg");
            FileOutputStream outCrop1 = new FileOutputStream(cropped1);
            blueArea.compress(Bitmap.CompressFormat.JPEG, 80, outCrop1);

            outCrop1.flush();
            outCrop1.close();
         }
         catch(FileNotFoundException e)
         {
            Log.d("In Saving File", e + "");    
         }
         catch(IOException e)
         {
            Log.d("In Saving File", e + "");
         }

         camera.startPreview();
         newImage.recycle();
         newImage = null;
         setSafeToTakePhoto(true);
    }

}

0 个答案:

没有答案