我认为@drew010 对他们的按钮的帮助很棒!谢谢你。
为了解决 SimpleCaptcha (16000Hz) 和 Flash 播放器支持的音频 (11025Hz) 之间的速率频率支持的相同问题,我使用了另一种解决方案,而不是更改 simplecaptcha 源或尝试在 Flash 编程中解决它。
我的验证码实现有一个自定义音频验证码 servlet(由 simplecaptcha 支持),所以我在这篇文章中使用了@Claude 提供的提示,使用tritonus lib 来转换音频采样率。这段代码如下所示:
AudioFormat outDataFormat = new AudioFormat((float) 11025.0, (int) 16, (int) 1, true, false);
AudioInputStream lowResAIS = AudioSystem.getAudioInputStream(outDataFormat, audioCapthca.getChallenge().getAudioInputStream());
我的验证码 servlet 代码如下:
public class AudioCaptchaServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
Map<Integer, String[]> voicesMap = new HashMap<Integer, String[]>();
String[] fileLocs0 = {"/captcha/pt_BR/0.wav"};
String[] fileLocs9 = {"/captcha/pt_BR/1.wav"};
(...)
voicesMap.put(0, fileLocs0);
voicesMap.put(1, fileLocs1);
(...)
VoiceProducer voiceProducer = new RandomNumberVoiceProducer(voicesMap);
AudioCaptcha ac = new AudioCaptcha.Builder()
.addAnswer()
.addNoise()
.addVoice(voiceProducer)
.build();
req.getSession().setAttribute(AudioCaptcha.NAME, ac.getAnswer());
//Converte Sons com bitrate de 16000Hz (simplecaptcha) para 11025Hz (suportada pelo flash player)
AudioFormat outDataFormat = new AudioFormat((float) 11025.0, (int) 16, (int) 1, true, false);
AudioInputStream lowResAIS = AudioSystem.getAudioInputStream(outDataFormat, ac.getChallenge().getAudioInputStream());
CaptchaServletUtil.writeAudio(resp, new Sample(lowResAIS));
}
@Override protected void doPost(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}