java– 通过摄像头拍摄照片并以bytearray形式发送到服务器

java– 通过摄像头拍摄照片并以bytearray形式发送到服务器,第1张

概述我正在开发一个Andorid应用程序,我希望用户在其中拍照,然后保存它我将它发送到服务器.现在,我将图片作为字节数组发送到服务器.当我尝试将字节数组作为PNG文件保存到文件中,然后尝试打开文件时,图像查看器会抱怨PNG有错误而无法显示.PNG文件大小为122Kb.当我尝试使用Scalr库来调

我正在开发一个AndorID应用程序,我希望用户在其中拍照,然后保存它我将它发送到服务器.现在,我将图片作为字节数组发送到服务器.当我尝试将字节数组作为PNG文件保存到文件中,然后尝试打开文件时,图像查看器会抱怨PNG有错误而无法显示. PNG文件大小为122Kb.

当我尝试使用Scalr库来调整图像大小时,它表示图像源不能为空.直接保存字节数会导致PNG损坏.我应该如何将文件正确转换并发送到服务器,因此没有错误.
这是我正在使用并发送它的相机代码.

public class AddPhotoForUser extends DrawerLoader {    private static final int CAMERA_PIC_REQUEST = 22;    button BtnSelectimage;    private ImageVIEw imgPhoto;    private static volatile Bitmap photo;    private static volatile ByteArrayOutputStream stream = new ByteArrayOutputStream();    final PersonServiceImpl personService = new PersonServiceImpl();    private String[] navMenuTitles;    private TypedArray navMenuIcons;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.upload_user_photo);        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);        navMenuIcons = getResources()                .obtainTypedArray(R.array.nav_drawer_icons);        set(navMenuTitles, navMenuIcons);        button uploadImagebutton = (button) findVIEwByID(R.ID.uploadUserImagebutton);        uploadImagebutton.setVisibility(VIEw.INVISIBLE);        imgPhoto = (ImageVIEw) findVIEwByID(R.ID.userPhotoImageVIEw);        BtnSelectimage = (button) findVIEwByID(R.ID.userPhotobuttonSelect);        BtnSelectimage.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                try {                    Intent cameraIntent = new Intent(androID.provIDer.MediaStore.ACTION_IMAGE_CAPTURE);                    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);                } catch (Exception e) {                    Toast.makeText(getApplicationContext(), "Couldn't load photo", Toast.LENGTH_LONG).show();                }            }        });        uploadImagebutton.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                if (!(v == null)) {                    uploadImage();                    try {                        Thread.sleep(5000);                    } catch (InterruptedException e) {                        e.printstacktrace();                    }                    Intent intent = new Intent(AddPhotoForUser.this, RestaurantList.class);                    startActivity(intent);                    finish();                }            }        });    }    @OverrIDe    public voID onBackpressed() {        Intent intent = new Intent(AddPhotoForUser.this, Login.class);        startActivity(intent);        finish();    }    @OverrIDe    public voID onActivityResult(final int requestCode, int resultCode, Intent data) {        try {            switch (requestCode) {                case CAMERA_PIC_REQUEST:                    if (resultCode == RESulT_OK) {                        try {                            photo = (Bitmap) data.getExtras().get("data");                            if (!(photo == null)) {                                imgPhoto.setimageBitmap(photo);                                button uploadImagebutton = (button) findVIEwByID(R.ID.uploadUserImagebutton);                                uploadImagebutton.setVisibility(VIEw.VISIBLE);                            }                        } catch (Exception e) {                            Toast.makeText(this, "Couldn't load photo", Toast.LENGTH_LONG).show();                        }                    }                    break;                default:                    break;            }        } catch (Exception e) {            e.printstacktrace();        }    }    private voID uploadImage() {        if (!(photo == null)) {            photo.compress(Bitmap.CompressFormat.PNG, 100, stream);            byte[] byteArray = stream.toByteArray();            personService.addUserProfilePhoto(byteArray);        }    }}

以下是将映像保存到磁盘的服务器端代码:

 @OverrIDe    public Boolean updateProfilePhoto(byte[] photo) {        Person person = getCurrentlyAuthenticatedPerson();        try{           inputStream in = new ByteArrayinputStream(photo);            BufferedImage image = ImageIO.read(in);            image = Scalr.resize(image, Scalr.Method.QUAliTY, 100, 100);            ByteArrayOutputStream baos = new ByteArrayOutputStream();            ImageIO.write(image, "png", baos);            baos.flush();            file file = new file(userImagePath);            if (file.exists() && file.isDirectory()) {                OutputStream outputStream = new fileOutputStream(new file(userImagePath + person.getUserID()+".png"));                outputStream.write(baos.toByteArray());                outputStream.close();            } else {                file file1 = new file(userImagePath+person.getUserID()+".png");                if (file1.exists()) {                    try {                        OutputStream outputStream = new fileOutputStream(file1);                        outputStream.write(baos.toByteArray());                        outputStream.close();                    } catch (IOException e) {                        e.printstacktrace();                    }                } else {                   boolean result = file1.createNewfile();                    System.out.println("Result of file1 creation is "+result);                    OutputStream outputStream = new fileOutputStream(file1);                    outputStream.write(baos.toByteArray());                    outputStream.close();                }            }            return true;        }catch (Exception e){            e.printstacktrace();            return false;        }    }

如果我不使用Scalr库,则没有错误,但它是一个损坏的文件.这是Scalr错误日志:

java.lang.IllegalArgumentException: src cannot be null    at org.imgscalr.Scalr.resize(Scalr.java:1564)    at org.imgscalr.Scalr.resize(Scalr.java:1415)    at com.journaldev.spring.service.PersonServiceImpl.updateProfilePhoto(PersonServiceImpl.java:84)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:497)    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)    at org.springframework.transaction.interceptor.TransactionInterceptor.proceeDWithInvocation(TransactionInterceptor.java:98)    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)    at com.sun.proxy.$Proxy51.updateProfilePhoto(UnkNown Source)    at com.journaldev.spring.Controller.PersonController.addProfilePhotoForUser(PersonController.java:100)

你能帮忙的话,我会很高兴.非常感谢. 总结

以上是内存溢出为你收集整理的java – 通过摄像头拍摄照片并以bytearray形式发送到服务器全部内容,希望文章能够帮你解决java – 通过摄像头拍摄照片并以bytearray形式发送到服务器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1099625.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存