也许这个问题重复了另一个问题,但其他问题我还没有找到解决方案。我正在尝试编写一个自定义的 android 相机,相机在我的设备(HTC One)上工作正常。我尝试了一些不同的设备,并在其中一个设备上工作。但我在Samsung
设备上遇到问题。不要关闭设备上的自动对焦功能。因此,设备不能改变焦点区域。那是我的触摸事件。
@Override
public boolean onTouchEvent(final MotionEvent event){
Camera.Parameters cameraParameters = camera.getParameters();
if (event.getAction() == MotionEvent.ACTION_UP){
focusAreas.clear();
meteringAreas.clear();
Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f);
Rect meteringRect = calculateTapArea(event.getX(), event.getY(), 1.5f);
focusAreas.add(new Camera.Area(focusRect, 800));
meteringAreas.add(new Camera.Area(meteringRect, 800));
cameraParameters.setFocusAreas(focusAreas);
cameraParameters.setMeteringAreas(meteringAreas);
cameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
try{
camera.setParameters(cameraParameters);
} catch(Exception e){
Log.e("Focus problem", e.toString());
return false;
}
camera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
camera.cancelAutoFocus();
Camera.Parameters params = camera.getParameters();
if(params.getFocusMode() != Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE){
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
camera.setParameters(params);
}
}
});
focusSound = new MediaPlayer();
showSquareFocus();
try {
AssetFileDescriptor descriptor = this.getApplicationContext().getAssets()
.openFd("focus.wav");
focusSound.setDataSource(descriptor.getFileDescriptor(),
descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
focusSound.prepare();
focusSound.setLooping(false);
focusSound.start();
focusSound.setVolume(10,10);
focusSound.setOnCompletionListener(new OnCompletionListener(){
public void onCompletion(MediaPlayer mp){
mp.release();
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
也许需要这个代码......
private Rect calculateTapArea(float x, float y, float coefficient) {
int areaSize = Float.valueOf(FOCUS_AREA_SIZE * coefficient).intValue();
int left = clamp((int) x - areaSize / 2, 0, width - areaSize);
int top = clamp((int) y - areaSize / 2, 0, height - areaSize);
RectF rectF = new RectF(left, top, left + areaSize, top + areaSize);
matrix.mapRect(rectF);
return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right), Math.round(rectF.bottom));
}
private int clamp(int x, int min, int max) {
if (x > max) {
return max;
}
if (x < min) {
return min;
}
return x;
}