[sane-devel] add support for ricoh aficio sp100su

viruxx dj_virus142 at hotmail.com
Mon Mar 13 21:51:56 UTC 2017


//with an GUI it's better <3

viruxx wrote
> package me.virux;
> 
> import java.awt.BorderLayout;
> import java.awt.Color;
> import java.awt.Dimension;
> import java.awt.GridLayout;
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
> import java.awt.image.BufferedImage;
> import java.io.File;
> import java.io.IOException;
> import java.nio.ByteBuffer;
> import java.nio.ByteOrder;
> import java.nio.IntBuffer;
> import java.util.Date;
> 
> import javax.imageio.ImageIO;
> import javax.swing.JButton;
> import javax.swing.JCheckBox;
> import javax.swing.JFileChooser;
> import javax.swing.JFrame;
> import javax.swing.JLabel;
> import javax.swing.JPanel;
> import javax.swing.JProgressBar;
> import javax.swing.JTextArea;
> import javax.swing.JTextField;
> 
> import org.usb4java.BufferUtils;
> import org.usb4java.Context;
> import org.usb4java.DeviceHandle;
> import org.usb4java.LibUsb;
> 
> public class AficioSP100su { 
> 	
> 	static File record;static JFrame frame = new JFrame("Julietta scanner"); 
> 	 static JCheckBox boxblack = new JCheckBox("pure black"), 
> 			 boxquality = new JCheckBox("hi quality"),
> 			 boxcolor = new JCheckBox("color"), boxjpeg = new JCheckBox("jpeg");
> 	 static JProgressBar bar = new JProgressBar();
> 	 static JButton scan = new JButton("scan");
>   	static DeviceHandle usb;  
>  	static boolean filechoosed = false,
>  			 cancel=false ; 
>  	static JFileChooser chooser = new JFileChooser();
>  	 static JTextArea textarea = new JTextArea("Welcome to Julietta
> Scanner"); 
>  	static JTextField height = new JTextField("3510");
>   	static JLabel label = new JLabel("height:");
>   	static JPanel chooserpane = new JPanel();
>   	
>   	
> 	public static void main(String[] args) throws IOException {  
> 		
> 		
>         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
>         
>         ActionListener listener = new ActionListener(){ 
> 			@Override public void actionPerformed(ActionEvent button) {
> 				if(button.getSource().equals(boxcolor) && boxcolor.isSelected()==true) 
> 					boxblack.setSelected(false);
> 				if(button.getSource().equals(boxblack) && boxblack.isSelected()==true) 
> 					boxcolor.setSelected(false); 
> 				if(button.getSource().equals(chooser) ){
> 					 if(button.getSource().toString().contains("APPROVE_OPTION")){
> 						filechoosed=true;
>  				     	textarea.setText(chooser.getSelectedFile().getAbsolutePath());
> 					 }
> 					 else 
> 						cancel=true;
> 				     chooserpane.remove(chooser);
> 				     frame.repaint();frame.revalidate();
> 				}
> 				if(button.getSource().equals(scan)){
> 					enableChooser();
> 
> 					
> 					new Thread(){public void run(){
> 								scan.setEnabled(false);
> 								try {
> 									scan(Integer.parseInt(height.getText()));
> 								} catch (Exception e)
> {scan.setEnabled(true);chooserpane.remove(chooser);frame.repaint();frame.revalidate();
> 									textarea.setText("error permission or no aficio sp100su detected
> "+e.getMessage());}  
> 							}}.start(); 
> 				}
> 				 
> 			}
> 
> 			
>         }; 
> 		boxcolor.addActionListener(listener); 
> chooser.addActionListener(listener); 
> 		boxblack.addActionListener(listener);  scan.addActionListener(listener); 
> 		
> 		chooserpane.setPreferredSize(new Dimension(510,320)); 
>         frame.getContentPane().add(chooserpane, BorderLayout.PAGE_START); 
>         frame.getContentPane().add(scan, BorderLayout.LINE_START);
>         frame.getContentPane().add(getBoxesPanel(),
> BorderLayout.LINE_END);
>         frame.getContentPane().add(getBottomPanel(),
> BorderLayout.PAGE_END); 
>         frame.pack();
>         frame.setVisible(true);
>    }//end of main program
> 	
>    static void enableChooser() {
> 	   textarea.setText("file ?");
> 	   String path = chooser.getCurrentDirectory().getAbsolutePath();
> 		chooser.setSelectedFile(new File(path+"/sexyFileName "+getTime() ));
> 		chooser.setFileFilter(null);
> 		chooserpane.add(chooser);
>    }
> 	
> 	
> 	private static String getTime() {
> 		Date date = new Date();
> 		int min = date .getMinutes();
> 		int sec = date.getSeconds();
> 		return min+"-"+sec;
> 	}
> 
> 	static JPanel getBoxesPanel(){
> 	  	JPanel boxarea = new JPanel(); GridLayout layout = new GridLayout();
> 		layout.setColumns(1); layout.setRows(4); boxarea.setLayout(layout);
> 		boxjpeg.setSelected(true); boxcolor.setSelected(true);
> boxquality.setSelected(true); 
> 	      
> 	    boxarea.add(boxcolor); boxarea.add(boxquality);
> 	    boxarea.add(boxblack); boxarea.add(boxjpeg);
> 	    boxarea.add(label); boxarea.add(height);
> 	    return boxarea;
> 	}
> 	
> 	static JPanel getBottomPanel(){
> 		JPanel panel = new JPanel(); GridLayout layout = new GridLayout();
> 		layout.setColumns(1); layout.setRows(2); panel.setLayout(layout); 
> 		panel.add(textarea); panel.add(bar);
> 		return panel;
> 	}
> 	
> 	
> 	
> 	static void scan(int height) throws Exception{
> 		filechoosed = false;
> 		cancel=false; 
> 		int width = 2560, packet = 61440; 
> 		if(boxquality.isSelected()){ width *= 2; height *= 2; }  
> 		int bytesToRead = width * height, totalBytesRead = 0; 
> 		if(boxcolor.isSelected()) bytesToRead *= 3; 
> 		bar.setMaximum(bytesToRead);bar.setValue(0);
> 		Context c = new Context();//start usb
> 		LibUsb.init(c);
> 		usb = LibUsb.openDeviceWithVidPid(c, (short)0x05ca, (short)0x042c);
> 		LibUsb.claimInterface(usb, 0); 
> 		 
> 		send(new byte[]{0x03,0x09,0x01});    //start urb procedure
> 		read(1);
> 		send(new byte[]{0x03,0x0d,0x0b}); 
> 		read(11); 
> 		byte[] sequence = new
> byte[]{0x03,0x0c,0x11,0,0,0,0x01,0x02,0x05,0xffffffff,0,0,0,0,0xffffffec,0x13,0x6c,0x1b};
> 		if(boxquality.isSelected())
> 			sequence[6]=0x02; 
> 		if(boxcolor.isSelected())
> 			sequence[7]=0x03;
> 		send(sequence); 
> 		send(new byte[]{0x03,0x0b,0x08});
> 		read(8);
> 		send(new byte[]{0x03,0x08,0x04,0,0,0,0,0x50,0x6d,0x06,0x01});
> 		 
> 		//#######main loop
> 		ByteBuffer buffer = ByteBuffer.allocateDirect(bytesToRead);
>  		 	boolean loop = true; 
>  		 	while(loop){ bar.setValue(totalBytesRead);
>  		 		
>  		 		send(new byte[]{0x03, 0x0E, 0x04, 0,0,0,0,(byte) (packet/256) });
> //give me next packet pls 
> 			   
>  		 		buffer.put( read(packet) );
>  		 		totalBytesRead+=packet; 
> 			 
>  		 		if(totalBytesRead + packet >  bytesToRead) 
>  		 			packet = bytesToRead - totalBytesRead;
>  		 			 
>  		 		if(totalBytesRead + packet > bytesToRead || packet == 0 || cancel) 
>  		 			loop = false; 
>  		 	}//end of main loop
> 		 
> 		 send(new byte[]{0x03,0x0a}); //send cancel
> 		 send(new byte[]{0x03,0x09,0x01}); //send end 
> 		 read(1); //dont forget response
> 		
> 		 //no need usb anymore
> 		 LibUsb.releaseInterface(usb, 0);
> 		 LibUsb.close(usb);
> 		 scan.setEnabled(true);
> 		 while(!filechoosed && !cancel)
> 				 Thread.sleep(1000); 
> 		 chooserpane.remove(chooser); frame.repaint();frame.revalidate();
> 		//########## data interpretation 
> 		int passed = 0; BufferedImage img = null;
> 		bar.setMaximum(totalBytesRead);bar.setValue(0);
> 		if(boxcolor.isSelected()){ //storage of aficio sp100 is 1 line = 1 color
> channel
> 			img = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
> 			
> 			for(int i = 0 ; i < totalBytesRead - (width * 3) ; i++ ){//loop the
> buffer until last line -2
> 				if(i % width == 0){ //if 1 complete width/line readed, jump over 2
> lines/width
> 					i += width * 2 ; 
> 					passed += width * 2 ; 
> 				}bar.setValue(i);
> 				if(cancel )return;
> 				int r = buffer.get(i) & 0xFF;//first color is at current position
> 				int g = buffer.get(i + width) & 0xFF;//second color is at next
> line/width
> 				int b = buffer.get(i + width + width) & 0xFF;//3rd color is 2 lines
> after
> 				Color col = new Color( g,b,r ); //mix the 3 bytes in THIS order /!\
> 				img.setRGB( (i-passed) % width, (i-passed) / width, col.getRGB());
> //place pixel using line position
> 			}   
> 		}else{
> 			img = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);
> 			
> 			if(boxblack.isSelected())//if black selected, software push it here
> 				img = new BufferedImage(width, height,
> BufferedImage.TYPE_BYTE_BINARY);
> 			
> 			for(int i = 0 ; i < totalBytesRead ; i++ ) {bar.setValue(i);if(cancel
> )return;
> 				int gray = buffer.get(i) & 0xFF;
> 				img.setRGB(i % width, i / width, new Color(gray, gray, gray).getRGB()
> ); 
> 			}
> 		}
> 		if(cancel )return;
> 		 String path = textarea.getText();
> 		if(boxjpeg.isSelected())
> 			ImageIO.write(img, "jpeg", new File(path + ".jpg"));  
> 		else
> 			ImageIO.write(img, "png", new File(path + ".png"));  
> 		
> 	}
> 
> //###########################################################################
> 	static void send(byte[] text) {  
> 		ByteBuffer buffer = ByteBuffer.allocateDirect( 64 ); 
> 		buffer.put(text);  
> 		LibUsb.bulkTransfer(usb, (byte) 0x03, buffer, IntBuffer.allocate(3),
> 20000);
> 		 
> 	}
> 	
> 
> //###########################################################################
> 	static ByteBuffer read(int size) {
> 		ByteBuffer buffer = BufferUtils.allocateByteBuffer(size).order(
> ByteOrder.LITTLE_ENDIAN); 
>         LibUsb.bulkTransfer(usb, (byte) 0x85, buffer,
> IntBuffer.allocate(3), 20000); 
>         return buffer;
>     }
> }





--
View this message in context: http://sane.10972.n7.nabble.com/add-support-for-ricoh-aficio-sp100su-tp20705p20719.html
Sent from the SANE - Dev mailing list archive at Nabble.com.



More information about the sane-devel mailing list