0

public class MainActivity extends 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();
                }
            }
        }

    }

}

I was try to decode uri and send it through socket to do detection

button1 is to accessing the camera. button2 is sending the data array to socket

The // marks i was just convert picture to bitmap from the camera but i need higher resolution picture to send through the socket

  • i had try the Uri imagepath=get.data(); Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imagepath); under onActivityResult but getBitmap return error – Beginner Nov 21 '20 at 21:06
  • The imageView.setImageURI(image_uri) shown the picture I just kinda confuse what is URI – Beginner Nov 21 '20 at 21:20
  • try this answer https://stackoverflow.com/questions/3879992/how-to-get-bitmap-from-an-uri – aziz k'h Nov 21 '20 at 22:10
  • Yeah i look forward that too but i just kinda dont understand why using openConnection – Beginner Nov 22 '20 at 01:58

0 Answers0