Java 我想在相机拍摄的图像上随机画一个圆形或椭圆形

Java 我想在相机拍摄的图像上随机画一个圆形或椭圆形,java,android,Java,Android,我的问题是,我将从相机中拍摄一张图像,并将其发送到活动中的图像视图。之后,我需要在图像上画一个圆或矩形。它应该是一个自由的手。我的意思是我想画任何尺寸的画。然后应该保存该图像。我试着把图像放在视图上,但我无法在上面画画。我是android新手。有人能帮我摆脱这一切吗 //This is camera Intent camera =(Button)findViewById(R.id.camera_button); camera.setOnClickListen

我的问题是,我将从相机中拍摄一张图像,并将其发送到活动中的图像视图。之后,我需要在图像上画一个圆或矩形。它应该是一个自由的手。我的意思是我想画任何尺寸的画。然后应该保存该图像。我试着把图像放在视图上,但我无法在上面画画。我是android新手。有人能帮我摆脱这一切吗

  //This is camera Intent

        camera =(Button)findViewById(R.id.camera_button);
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rl_main.setVisibility(View.GONE);
                rl_image.setVisibility(View.VISIBLE);

                Intent intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                fileUri = getOutputMediaFileUri(REQUEST_IMAGE_CAPTURE);

                intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                // start the image capture Intent
                startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);


            }


        });

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case SPEECH_RECOGNITION_CODE: {
                if (resultCode == RESULT_OK && null != data) {

                    ArrayList<String> result = data
                            .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    String text = result.get(0);
                    description.setText(text);
                }
                break;
            }

            case REQUEST_IMAGE_CAPTURE:
            {
                if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                    loadimage();
                }
            }


        }

    }

    private void loadimage() {

        if (fileUri!=null){

            Bitmap myImg = BitmapFactory.decodeFile(fileUri.getPath());

            File file = new File(fileUri.getPath());
            BitmapFactory.Options bounds =new BitmapFactory.Options();
            bounds.inSampleSize = 4;
            bounds.inJustDecodeBounds = true;
            String path = file.getPath();
            BitmapFactory.decodeFile(path,bounds);

            BitmapFactory.Options opts =new BitmapFactory.Options();
            opts.inSampleSize = 4;
            Bitmap bm = BitmapFactory.decodeFile(path,opts);
            ExifInterface exif =null;

            try {
                exif =new ExifInterface(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
            int orientation = orientString != null ? Integer.parseInt(orientString) :  ExifInterface.ORIENTATION_NORMAL;

            int rotationAngle = 0;
            if (orientation == ExifInterface.ORIENTATION_ROTATE_90){
                rotationAngle = 90;
            }
            if (orientation == ExifInterface.ORIENTATION_ROTATE_180){
                rotationAngle = 180;
            }
            if (orientation == ExifInterface.ORIENTATION_ROTATE_270){
                rotationAngle = 270;
            }


            final Matrix matrix = new Matrix();
            matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
            Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth,bounds.outHeight, matrix, true);


            /*    Matrix matrix = new Matrix();
               matrix.postRotate(90);
                Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
                        matrix, true);*/
            Bitmap mutableBitmap = rotatedBitmap.copy(Bitmap.Config.ARGB_8888, true);


            camera_IV.setImageBitmap(mutableBitmap);

            DisplayMetrics displaymetrics =new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int height1 = displaymetrics.heightPixels;
            int width1 = displaymetrics.widthPixels;
            Log.e("MA", "Height= " + height1 + " " + width1);
            paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.GREEN);
            paint.setStrokeWidth(3);
            //  paint.setTextSize(25);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);
            originalBitmap =Bitmap.createScaledBitmap(rotatedBitmap, camera_IV.getWidth(), camera_IV.getHeight(), false);

            imageBitmap = originalBitmap.copy(Bitmap.Config.RGB_565, true);

            camera_IV.setOnDragListener(new View.OnDragListener() {
                @Override
                public boolean onDrag(View view, DragEvent dragEvent) {
                    return false;
                }
            });
            camera_IV.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View arg0, MotionEvent event) {



            });



            BitmapFactory.Options myOptions = new BitmapFactory.Options();
            myOptions.inDither = true;
            myOptions.inScaled = false;
            myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
            myOptions.inPurgeable = true;



            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(0xFFFF0000);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            // mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(10);

        }


    }
//这是摄像机的意图
摄像头=(按钮)findViewById(R.id.camera_按钮);
camera.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
rl_main.setVisibility(View.GONE);
rl_image.setVisibility(View.VISIBLE);
意向意向=新意向(MediaStore.ACTION\u IMAGE\u CAPTURE);
fileUri=getOutputMediaFileUri(请求图像捕获);
intent.putExtra(MediaStore.EXTRA_输出,fileUri);
//启动图像捕获计划
startActivityForResult(意图、请求、图像捕获);
}
});
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
开关(请求代码){
案例语音识别代码:{
if(resultCode==RESULT\u OK&&null!=数据){
ArrayList结果=数据
.getStringArrayListExtra(识别器意图.额外结果);
String text=result.get(0);
description.setText(文本);
}
打破
}
案例请求\u图像\u捕获:
{
if(requestCode==REQUEST\u IMAGE\u CAPTURE&&resultCode==RESULT\u OK){
loadimage();
}
}
}
}
私有void loadimage(){
if(fileUri!=null){
位图myImg=BitmapFactory.decodeFile(fileUri.getPath());
File File=新文件(fileUri.getPath());
BitmapFactory.Options bounds=新的BitmapFactory.Options();
bounds.inSampleSize=4;
bounds.InjustDecodeBunds=true;
字符串路径=file.getPath();
解码文件(路径、边界);
BitmapFactory.Options opts=新的BitmapFactory.Options();
opts.inSampleSize=4;
位图bm=BitmapFactory.decodeFile(路径,opts);
ExifInterface exif=null;
试一试{
exif=新的ExifInterface(路径);
}捕获(IOE异常){
e、 printStackTrace();
}
String-orientString=exif.getAttribute(ExifInterface.TAG\u-ORIENTATION);
int-orientString!=null?整数。parseInt(orientString):ExifInterface.orientation\u NORMAL;
int rotationAngle=0;
如果(方向==ExifInterface.orientation\u ROTATE\u 90){
旋转角度=90;
}
如果(方向==ExifInterface.orientation\u ROTATE\u 180){
旋转角度=180;
}
如果(方向==ExifInterface.orientation\u ROTATE\u 270){
旋转角度=270;
}
最终矩阵=新矩阵();
matrix.setRotate(rotationAngle,(float)bm.getWidth()/2,(float)bm.getHeight()/2);
位图旋转位图=位图.createBitmap(bm,0,0,bounds.outWidth,bounds.outHeight,matrix,true);
/*矩阵=新矩阵();
矩阵旋转后(90);
位图旋转=Bitmap.createBitmap(myImg,0,0,myImg.getWidth(),myImg.getHeight(),
矩阵,真)*/
Bitmap mutableBitmap=rotatedBitmap.copy(Bitmap.Config.ARGB_8888,true);
摄像机四、设置图像位图(可变位图);
DisplayMetrics DisplayMetrics=新的DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height1=displaymetrics.heightPixels;
int width1=displaymetrics.widthPixels;
Log.e(“MA”,“Height=“+height1+”“+width1”);
油漆=新油漆();
绘制.设置样式(绘制.样式.笔划);
油漆。设置颜色(颜色。绿色);
油漆。设置行程宽度(3);
//油漆.尺寸(25);
绘制.设置行程连接(绘制.连接.圆形);
油漆固定行程盖(油漆固定行程盖圆形);
originalBitmap=Bitmap.createScaledBitmap(旋转位图,camera_IV.getWidth(),camera_IV.getHeight(),false);
imageBitmap=originalBitmap.copy(Bitmap.Config.RGB_565,true);
camera_IV.setOnDragListener(新视图.OnDragListener(){
@凌驾
公共布尔onDrag(视图,DrageEvent DrageEvent){
返回false;
}
});
camera_IV.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图arg0,运动事件){
});
BitmapFactory.Options myOptions=新的BitmapFactory.Options();
myOptions.inDither=true;
myOptions.inScaled=false;
myOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//重要
myOptions.inpurgable=true;
mPaint=新油漆();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(油漆、样式、笔划);
mPaint.setStrokeJoin(油漆.连接.圆形);
//mPaint.setStrokeCap(油漆盖圆形);
mPaint.设定行程宽度(10);
}
}
`