在应用程序中模拟 WP7 相机胶卷缩放和拖动功能

发布于 2024-12-11 04:36:37 字数 3189 浏览 0 评论 0原文

我试图让用户拍摄包含文本的图片,以便用户可以突出显示他们想要运行 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 的第二周,所以我可能只是错过了一些简单的东西。提前致谢。

I am attempting to have the user take a picture of something containing text so that the user can highlight the text they would like to have an OCR run against.

I have the picture part down and I am able to display the image. I then would like the user to be able to pinch to zoom and then swipe to move the picture just like the camera roll does for an individual photo. This would allow the user to see the text they would like to run the OCR against more clearly. Currently I only have the pinch zoom working but really badly.

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);
}

I found this code from WindowsPhoneGeek so I will not take 100% credit for it. I actually lose the picture when I zoom too far out and it goes crazy by bouncing to different margins when I zoom out as well. I believe this could be because I have to rotate the image 90 degrees.

I would like to also have the user move the picture with a drag gesture. Anyone ever done anything like this or have any tips?

Also, this is only my 2nd week working with WP7 so I may just be missing something simple. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谎言 2024-12-18 04:36:37

http://multitouch.codeplex.com/ 上有一些预先存在的行为,这可能是一个更简单的解决方案满足您的需求。

There are some pre-existing behaviours at http://multitouch.codeplex.com/ which may be a simpler solution for your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文