我试图让用户拍摄包含文本的内容,以便用户可以突出显示他们想要进行OCR运行的文本。
我将图片部分放下,我可以显示图像。然后,我希望用户能够捏缩放,然后滑动以移动图片,就像相机胶卷对单张照片一样。这将允许用户更清楚地看到他们想要运行OCR的文本。目前我只有捏缩放工作,但非常糟糕。
private Popup popup = new Popup { IsOpen = true };
private Grid grid = new Grid { Width = 480, Height = 800 };
private Image backgroundImage = new Image
{
Stretch = Stretch.Uniform,
RenderTransformOrigin = new Point(.5, .5),
RenderTransform = new CompositeTransform()
};
void OCRSelection_Loaded(object sender, RoutedEventArgs e)
{
this.grid.Children.Add(this.backgroundImage);
this.popup.Child = this.grid;
var gl = GestureService.GetGestureListener(this.backgroundImage);
gl.PinchStarted += new EventHandler<PinchStartedGestureEventArgs(gl_PinchStarted);
gl.PinchDelta += new EventHandler<PinchGestureEventArgs>(gl_PinchDelta);
}
private void gl_PinchStarted(object sender, PinchStartedGestureEventArgs e)
{
_oldFinger1 = e.GetPosition(this.backgroundImage, 0);
_oldFinger2 = e.GetPosition(this.backgroundImage, 1);
_oldScaleFactor = 1;
}
private void gl_PinchDelta(object sender, PinchGestureEventArgs e)
{
var scaleFactor = e.DistanceRatio / _oldScaleFactor;
var currentFinger1 = e.GetPosition(this.backgroundImage, 0);
var currentFinger2 = e.GetPosition(this.backgroundImage, 1);
var translationDelta = GetTranslationDelta(
currentFinger1,
currentFinger2,
_oldFinger1,
_oldFinger2,
ImagePosition,
scaleFactor);
_oldFinger1 = currentFinger1;
_oldFinger2 = currentFinger2;
_oldScaleFactor = e.DistanceRatio;
UpdateImage(scaleFactor, translationDelta);
}
private void UpdateImage(double scaleFactor, Point delta)
{
TotalImageScale *= scaleFactor;
ImagePosition = new Point(ImagePosition.X + delta.X, ImagePosition.Y + delta.Y);
var transform = (CompositeTransform)this.backgroundImage.RenderTransform;
transform.ScaleX = TotalImageScale;
transform.ScaleY = TotalImageScale;
transform.TranslateX = ImagePosition.X;
transform.TranslateY = ImagePosition.Y;
}
private Point GetTranslationDelta( Point currentFinger1, Point currentFinger2, Point oldFinger1, Point oldFinger2,
Point currentPosition, double scaleFactor)
{
var newPos1 = new Point(
currentFinger1.X + (currentPosition.X - oldFinger1.X) * scaleFactor,
currentFinger1.Y + (currentPosition.Y - oldFinger1.Y) * scaleFactor);
var newPos2 = new Point(
currentFinger2.X + (currentPosition.X - oldFinger2.X) * scaleFactor,
currentFinger2.Y + (currentPosition.Y - oldFinger2.Y) * scaleFactor);
var newPos = new Point(
(newPos1.X + newPos2.X) / 2,
(newPos1.Y + newPos2.Y) / 2);
return new Point(
newPos.X - currentPosition.X,
newPos.Y - currentPosition.Y);
}
我在WindowsPhoneGeek中找到了这个代码,所以我不会100%的功劳。当我放大太远时,我实际上失去了画面,当我缩小时,它会因为弹跳到不同的边缘而变得疯狂。我相信这可能是因为我必须将图像旋转90度。
我想让用户用拖动手势移动图片。有没有人做过这样的事情或有任何提示?
此外,这只是我第二周使用WP7,所以我可能只是缺少一些简单的东西。提前谢谢。