我可以知道如何把uri转换成位图吗?

vxf3dgd4  于 2021-07-11  发布在  Java
关注(0)|答案(0)|浏览(264)

公共类mainactivity扩展了appcompatactivity{

private static final int PERMISSION_CODE=1000;
private static final int IMAGE_CAPTURE_CODE=1001;
private Button button1;
private Button button2;
private ImageView imageView;
private Bitmap b;
Uri image_uri;

public void sendTextMsg(DataOutputStream out,String msg) throws IOException{
    byte[] bytes=msg.getBytes();
    long len = bytes.length;
    //先發送長度,在發送内容
    out.writeLong(len);
    out.write(bytes);
}

public void sendImgMsg(DataOutputStream out) throws IOException{
    //發送圖片,將bitmap轉爲字節數
    //BitmapDrawable drawable = (BitmapDrawable)imageView.getDrawable();//can work
    //Bitmap bitmap = drawable.getBitmap();//can work
    //Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background);

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    b.compress(Bitmap.CompressFormat.PNG,100,bout);
    long len = bout.size();
    Log.i("sendImgMsg","len:"+len);
    out.write(bout.toByteArray());
}

public void takePicture(){
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(i,0);
}

private void openCamera(){
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE,"New Picture");
    values.put(MediaStore.Images.Media.DESCRIPTION,"From the camera");
    image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
    //Camera intent
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,image_uri);
    startActivityForResult(cameraIntent,IMAGE_CAPTURE_CODE);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 = (Button) findViewById(R.id.Capture);//指定拍照button的id
    button2 = (Button) findViewById(R.id.ButtonSend);//指定button的id
    imageView=(ImageView)findViewById(R.id.Picture);//指定imageView的id

    button1.setOnClickListener(new View.OnClickListener() { //拍照的button
        @Override
        public void onClick(View v) {
            //takePicture();original shit
            if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
                if(checkSelfPermission(Manifest.permission.CAMERA) ==
                        PackageManager.PERMISSION_DENIED||
                        checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
                        PackageManager.PERMISSION_DENIED){
                    //permission not enabled,request it
                    String[] permission ={Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE};
                    //show popup to request permission
                    requestPermissions(permission, PERMISSION_CODE);
                }
                else {
                    //permission already granted
                    openCamera();
                }
            }
            else{
                openCamera();
            }
        }
    });

    button2.setOnClickListener(new View.OnClickListener() { //按下去的動作的event
        @Override
        public void onClick(View v) { //按下去
            new Thread() {
                Socket socket;
                String host = "192.168.0.101";
                int post = 8004;

                public void run() {
                    try {
                        socket = new Socket(host, post);
                        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                        sendImgMsg(out);
                        out.close();
                        socket.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
        });
    }

@Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if(resultCode==RESULT_OK) {
            //b=(Bitmap)data.getExtras().get("data");
            //imageView.setImageBitmap(b);
            //set the image captured to ImageView

            imageView.setImageURI(image_uri);
            }
        }

        //handling permission result
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults){
    switch (requestCode){
        case PERMISSION_CODE:{
            if(grantResults.length > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
                //permission from popup was granted
                openCamera();
            }
            else{
                //permission from popup was denied
                Toast.makeText(this,"Permission denied...",Toast.LENGTH_SHORT).show();
                }
            }
        }

    }

}

我试图解码uri并通过套接字发送它来进行检测
按钮1用于进入摄像机。button2正在将数据数组发送到socket
标记我刚刚转换图片到位图从相机,但我需要更高的分辨率图片发送通过插座

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题