我试图将从代号签名中捕获的签名上传到我的php服务器。问题是上传的图像是黑色图像。下面是我的代码。我可以修复此问题
SignatureComponent sig = new SignatureComponent();
sig.addActionListener((evt)-> {
try{
img = sig.getSignatureImage();
}catch(Exception ex){
ex.printStackTrace();
}
// Now we can do whatever we want with the image of this signature.
});
Button sv = new Button("save");
sv.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt) {
try {
Label it = new Label();
it.setIcon(img);
orderHome.add(it);
ImageIO imgIO= ImageIO.getImageIO();
ByteArrayOutputStream out = new ByteArrayOutputStream();
imgIO.save(img, out,ImageIO.FORMAT_JPEG, 1);
byte[] ba = out.toByteArray();
MultipartRequest request = new MultipartRequest();
String url = Global.url1 + "upload_photo.php";
request.setUrl(url);
request.addData("file",ba,"image/jpeg");
request.addArgument("order_id", order_id);
request.addArgument("customer_id", customer_id);
NetworkManager.getInstance().addToQueue(request);
和php代码
[![image uploaded][1]][1]
<?php
@SESSION_START();
require_once("../includes/functions.php");
$target_path="../uploads/";
$customer_id=$_REQUEST['customer_id'];
$order_id=$_REQUEST['order_id'];
$uid = uniqid();
$file =$uid.".jpg";
$sucess=move_uploaded_file($_FILES["file"]["tmp_name"], $target_path.$file);
黑色img是上传到服务器的文件。另一个显示正在运行的应用程序的屏幕截图。我想上传签名,如截图所示
答案 0 :(得分:1)
The signature generates a translucent image. JavaSE has some issues with saving translucent images as JPEGs and thus PNG works well. Another alternative would be to create an opaque image and save that as a JPEG e.g.:
Image myImage = Image.create(img.getWidth(), img.getHeight());
myImage.getGraphics().drawImage(img, 0, 0);
The new myImage
will be opaque with the white color background.